﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TireService.Data
{
    public partial class TireServiceDB
    {

        //partial void UpdateCar(Car instance)
        //{
        //    this.ExecuteDynamicUpdate(instance);
        //}
        #region User

        

        #endregion
        
        #region Customer

        /// <summary>
        /// Helper method to return all customers
        /// </summary>
        /// <returns>List with Customer objects</returns>
        public List<Customer> GetCustomers()
        {
            return this.Customers.ToList();
        }

        /// <summary>
        /// Helper method to return all customers
        /// </summary>
        /// <returns>List with Customer objects</returns>
        public List<CustomerView> GetCustomersView()
        {
            return this.CustomerViews.ToList();
        }

        /// <summary>
        /// Helper method to return all customers for a specific parent customer
        /// </summary>
        /// <param name="ParentCustomerId">Id on parentcustomer</param>
        /// <returns>List with Customer objects</returns>
        public List<CustomerView> GetCustomersForParentCustomerView(int ParentCustomerId)
        {
            return this.CustomerViews.Where(p => p.ParentCustomerId == ParentCustomerId).ToList();
        }

        /// <summary>
        /// Helper method to return all business customers
        /// </summary>
        /// <returns>List with Customer objects</returns>
        public List<Customer> GetBusinessCustomers()
        {
            return this.Customers.Where(p => p.IsCompany == true).ToList();
        }

        /// <summary>
        /// Helper method to return all parent company customers
        /// </summary>
        /// <returns>List with Customer objects</returns>
        public List<Customer> GetParentCustomers()
        {
            return this.Customers.Where(p => p.IsCompany == true & p.ParentCustomerId == null).ToList();
        }

        /// <summary>
        /// Helper method to return customers under a specific parent customers
        /// </summary>
        /// <param name="ParentCustomerId">Id on parentcustomer</param>
        /// <returns>List with Customer objects</returns>
        public List<Customer> GetCustomersForParentCustomer(int ParentCustomerId)
        {
            return this.Customers.Where(p => p.ParentCustomerId == ParentCustomerId).ToList();
        }

        /// <summary>
        /// Helper method to return Customer object
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <returns>Customer Object</returns>
        public Customer GetCustomer(int CustomerId)
        {
            return this.Customers.SingleOrDefault(p => p.CustomerId == CustomerId);
        }

        /// <summary>
        /// Helper method to return Customer object
        /// </summary>
        /// <param name="UserId">Id on login user</param>
        /// <returns>Customer Object</returns>
        public Customer GetCustomer(Guid UserId)
        {
            return this.Customers.SingleOrDefault(p => p.UserId == UserId);
        }

        /// <summary>
        /// Helper method to return Customer name
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <returns>String with customer name</returns>
        public string GetCustomerName(int CustomerId)
        {
            return this.Customers.SingleOrDefault(p => p.CustomerId == CustomerId).Name;
        }

        /// <summary>
        /// Helper method to return Customer name
        /// </summary>
        /// <param name="UserId">Id on login user</param>
        /// <returns>String with customer name</returns>
        public string GetCustomerName(Guid UserId)
        {
            return this.Customers.SingleOrDefault(p => p.UserId == UserId).Name;
        }

        /// <summary>
        /// Helper method to return True/False if customer should be synced with E-conomic
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <returns>True/False if customer should be synced with E-conomic</returns>
        public bool GetCustomerSyncToEconomic(int CustomerId)
        {
            return this.Customers.SingleOrDefault(p => p.CustomerId == CustomerId).SyncToEconomic;
        }

        /// <summary>
        /// Helper method to return Customer company name
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <returns>String with customer company name</returns>
        public string GetCustomerCompanyName(int CustomerId)
        {
            return this.Customers.SingleOrDefault(p => p.CustomerId == CustomerId).CompanyName;
        }

        /// <summary>
        /// Helper method to return Customer email
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <returns>String with customer email</returns>
        public string GetCustomerEmail(int CustomerId)
        {
            return this.Customers.SingleOrDefault(p => p.CustomerId == CustomerId).Email;
        }

        /// <summary>
        /// Helper method to return customer id for a specific userid
        /// </summary>
        /// <param name="UserId">Id on user (Guid)</param>
        /// <returns>Integer with customer id</returns>
        public int GetCustomerIdByUserId(Guid UserId)
        {
            return this.Customers.SingleOrDefault(p => p.UserId == UserId).CustomerId;
        }

        /// <summary>
        /// Helper function that adds a new Customer to the database
        /// </summary>
        /// <param name="customer">Customer object</param>
        public void AddCustomer(Customer customer)
        {
            Customers.InsertOnSubmit(customer);
        }

        /// <summary>
        /// Helper method that validates if parentcustomer id has access to a specific customerid
        /// </summary>
        /// <param name="CustomerId">Id on customer that parentcustomer try to access</param>
        /// <param name="ParentCustomerId">Id on parentcustomer</param>
        /// <returns>True/false value</returns>
        public bool ValidateParentCustomerAccess(int CustomerId, int ParentCustomerId)
        {
            if (this.Customers.SingleOrDefault(p => p.ParentCustomerId == ParentCustomerId & p.CustomerId == CustomerId) != null)
            {
                return true;
            }
            else
            {
                return false;
            };
        }

        /// <summary>
        /// Helper method to return emaillogs for a specific customer - show business view
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <param name="IncludeChildCustomers">True/false value if child customer log should be included</param>
        /// <returns>List with BusinessEmailLogListView objects</returns>
        public List<BusinessEmailLogListView> GetCustomerBusinessEmailLogs(int CustomerId, bool IncludeChildCustomers)
        {
            if (IncludeChildCustomers)
            {
                return this.BusinessEmailLogListViews.Where(p => p.CustomerId == CustomerId | p.ParentCustomerId == CustomerId).ToList();
            }
            else
            {
                return this.BusinessEmailLogListViews.Where(p => p.CustomerId == CustomerId).ToList();
            }
            
        }

        #endregion

        #region Currency

        /// <summary>
        /// Helper method to return all currencies
        /// </summary>
        /// <returns>List with currency objects</returns>
        public List<Currency> GetCurrencies()
        {
            return this.Currencies.ToList();
        }

        /// <summary>
        /// Helper method that returns currency code for a specific currencyid
        /// </summary>
        /// <param name="CurrencyId">Id for currency</param>
        /// <returns>String with currency code</returns>
        public string GetCurrencyCode(int CurrencyId)
        {
            return this.Currencies.SingleOrDefault(p => p.CurrencyId == CurrencyId).CurrencyCode;
        }

        #endregion

        #region Country

        /// <summary>
        /// Helper method to return all countries
        /// </summary>
        /// <returns>List with contry objects</returns>
        public List<Country> GetCountries()
        {
            return this.Countries.ToList();
        }

        /// <summary>
        /// Helper method that returns country name for a specific countryid
        /// </summary>
        /// <param name="CountryId">Id for country</param>
        /// <returns>String with country name</returns>
        public string GetCountryName(int CountryId)
        {
            return this.Countries.SingleOrDefault(p => p.CountryId == CountryId).CountryName;
        }

        #endregion

        #region CustomerSubscription

        /// <summary>
        /// Helper method to return all Subscriptions for a specific customer 
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <returns>List with CustomerSubscription objects</returns>
        public List<CustomerSubscription> GetCustomerSubscriptions(int CustomerId)
        {
            return this.CustomerSubscriptions.Where(p => p.CustomerId == CustomerId).ToList();
        }

        /// <summary>
        /// Helper method to return all Subscriptions that is active
        /// </summary>
        /// <returns>List with CustomerSubscription objects</returns>
        public List<CustomerSubscription> GetCustomerSubscriptionsActive()
        {
            return this.CustomerSubscriptions.Where(p => p.InActive == false).ToList();
        }

        /// <summary>
        /// Helper method to return all Subscriptions that is active for a specific customer
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <returns>List with CustomerSubscription objects</returns>
        public List<CustomerSubscription> GetCustomerSubscriptionsActive(int CustomerId)
        {
            return this.CustomerSubscriptions.Where(p => p.CustomerId == CustomerId & p.InActive == false).ToList();
        }

        /// <summary>
        /// Helper method to return all Subscriptions that must be renewed 
        /// </summary>
        /// <param name="Startdate">Startdate that next renewal date must be bigging than</param>
        /// <param name="Enddate">Enddate that next renewal date must be smaller than</param>
        /// <returns>List with CustomerSubscription objects</returns>
        public List<CustomerSubscription> GetCustomerSubscriptionsForRenewal(DateTime Startdate, DateTime Enddate)
        {
            return this.CustomerSubscriptions.Where(p => p.InActive == false & (p.NextRenewalDate > Startdate & p.NextRenewalDate < Enddate)).ToList();
        }

        /// <summary>
        /// Helper method to return all subscriptions for a specific customer - show business view
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <param name="IncludeChildCustomers">True/false value if child customer subscriptions should be included</param>
        /// <returns>List with BusinessSubscriptionListView objects</returns>
        public List<BusinessSubscriptionListView> GetCustomerBusinessSubscriptions(int CustomerId, bool IncludeChildCustomers)
        {
            if (IncludeChildCustomers)
            {
                return this.BusinessSubscriptionListViews.Where(p => p.CustomerId == CustomerId | p.ParentCustomerId == CustomerId).ToList();
            }
            else
            {
                return this.BusinessSubscriptionListViews.Where(p => p.CustomerId == CustomerId).ToList();
            }
            
        }

        /// <summary>
        /// Helper method to return CustomerSubscription object
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <param name="SubscriptionId">Id on subscription</param>
        /// <returns>CustomerSubscription Object</returns>
        public CustomerSubscription GetCustomerSubscription(int CustomerId, int SubscriptionId)
        {
            return this.CustomerSubscriptions.SingleOrDefault(p => p.CustomerId == CustomerId & p.SubscriptionId == SubscriptionId);
        }

        /// <summary>
        /// Helper method to return CustomerSubscription object
        /// </summary>
        /// <param name="CustomerSubscriptionId">Id on customersubscription</param>
        /// <returns>CustomerSubscription Object</returns>
        public CustomerSubscription GetCustomerSubscription(int CustomerSubscriptionId)
        {
            return this.CustomerSubscriptions.SingleOrDefault(p => p.CustomerSubscriptionId == CustomerSubscriptionId);
        }

        /// <summary>
        /// Helper method to return subscriptionid for a specific customersubscriptionid
        /// </summary>
        /// <param name="CustomerSubscriptionId">Id for customersubscription</param>
        /// <returns>Integer with SubscriptionId</returns>
        public int GetCustomerSubscriptionType(int CustomerSubscriptionId)
        {
            return this.CustomerSubscriptions.SingleOrDefault(p => p.CustomerSubscriptionId == CustomerSubscriptionId).SubscriptionId;
        }

        /// <summary>
        /// Helper function that adds a new CustomerSubscription to the database
        /// </summary>
        /// <param name="customersubscription">CustomerSubscription object</param>
        public void AddCustomerSubscription(CustomerSubscription customersubscription)
        {
            CustomerSubscriptions.InsertOnSubmit(customersubscription);
        }

        #region CustomerSubscriptionView 

        /// <summary>
        /// Helper method to return all Subscriptions
        /// </summary>
        /// <returns>List with CustomerSubscriptionView objects</returns>
        public List<CustomerSubscriptionView> GetCustomerSubscriptionViews()
        {
            return this.CustomerSubscriptionViews.ToList();
        }

        /// <summary>
        /// Helper method to return a customersubscription
        /// </summary>
        /// <param name="CustomerSubscriptionId">Id on customersubscription</param>
        /// <returns>CustomerSubscriptionView objects</returns>
        public CustomerSubscriptionView GetCustomerSubscriptionView(int CustomerSubscriptionId)
        {
            return this.CustomerSubscriptionViews.SingleOrDefault(p => p.CustomerSubscriptionId == CustomerSubscriptionId);
        }

        /// <summary>
        /// Helper method to return all Subscriptions for a specific customer 
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <returns>List with CustomerSubscriptionView objects</returns>
        public List<CustomerSubscriptionView> GetCustomerSubscriptionViews(int CustomerId)
        {
            return this.CustomerSubscriptionViews.Where(p => p.CustomerId == CustomerId).ToList();
        }

        #endregion

        #endregion

        #region CustomerSubscriptionLog

        /// <summary>
        /// Helper method to return all Subscriptions for a specific customer 
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <returns>List with CustomerSubscriptionLog objects</returns>
        public List<CustomerSubscriptionLog> GetCustomerSubscriptionLog(int CustomerId)
        {
            return this.CustomerSubscriptionLogs.Where(p => p.CustomerId == CustomerId).ToList();
        }

        /// <summary>
        /// Helper method to return list with CustomerSubscriptionLog objects
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <param name="SubscriptionId">Id on subscription</param>
        /// <returns>List with CustomerSubscriptionLog objects</returns>
        public List<CustomerSubscriptionLog> GetCustomerSubscriptionLog(int CustomerId, int SubscriptionId)
        {
            return this.CustomerSubscriptionLogs.Where(p => p.CustomerId == CustomerId & p.SubscriptionId == SubscriptionId).ToList();
        }

        /// <summary>
        /// Helper function that adds a new CustomerSubscriptionLog to the database
        /// </summary>
        /// <param name="customersubscriptionlog">CustomerSubscriptionLog object</param>
        public void AddCustomerSubscriptionLog(CustomerSubscriptionLog customersubscriptionlog)
        {
            CustomerSubscriptionLogs.InsertOnSubmit(customersubscriptionlog);
        }

        #endregion

        #region CustomerSalesReferenceHistory

        /// <summary>
        /// Helper function that adds a new CustomerSalesReferenceHistory to the database
        /// </summary>
        /// <param name="customersalesreferencehistory">CustomerSalesReferenceHistory object</param>
        public void AddCustomerSalesReferenceHistory(CustomerSalesReferenceHistory customersalesreferencehistory)
        {
            CustomerSalesReferenceHistories.InsertOnSubmit(customersalesreferencehistory);
        }

        #endregion

        #region Quotation

        /// <summary>
        /// Helper method to get quotation
        /// </summary>
        /// <param name="QuotationId">Id on quotation</param>
        /// <returns>Quotation object</returns>
        public Quotation GetQuotation(int QuotationId)
        {
            return this.Quotations.SingleOrDefault(p => p.QuotationId == QuotationId);
        }

        /// <summary>
        /// Helper method to get quotation
        /// </summary>
        /// <param name="QuotationId">Id on quotation</param>
        /// <param name="DateYear">Year for quotation</param>
        /// <param name="QuotationCode">Code for quotation</param>
        /// <returns>Quotation object</returns>
        public Quotation GetQuotation(int QuotationId, int DateYear, int QuotationCode)
        {
            return this.Quotations.SingleOrDefault(p => p.QuotationId == QuotationId & p.Createdate.Year == DateYear & p.QuotationCode == QuotationCode);
        }

        /// <summary>
        /// Helper method to return list with all quotations/vouchers send out by a customer
        /// </summary>
        /// <param name="CustomerId">Id on customer that has send out quotation/voucher</param>
        /// <returns>List with Quotation object</returns>
        public List<Quotation> GetCustomerQuotation(int CustomerId)
        {
            return this.Quotations.Where(p => p.ReferenceCustomerId == CustomerId).ToList();
        }

        /// <summary>
        /// Helper function that adds a new Quotation to the database
        /// </summary>
        /// <param name="quotation">Quotation object</param>
        public void AddQuotation(Quotation quotation)
        {
            Quotations.InsertOnSubmit(quotation);
        }

        #endregion

        #region QuotationDetail

        /// <summary>
        /// Helper method to return all quotation details for a specific quotation
        /// </summary>
        /// <param name="QuotationId">Id on quotation</param>
        /// <returns>List with QuotationDetail objects</returns>
        public List<QuotationDetail> GetQuotationDetails(int QuotationId)
        {
            return this.QuotationDetails.Where(p => p.QuotationId == QuotationId).ToList();
        }

        /// <summary>
        /// Helper function that adds a new QuotationDetail to the database
        /// </summary>
        /// <param name="quotationdetail">QuotationDetail object</param>
        public void AddQuotationDetail(QuotationDetail quotationdetail)
        {
            QuotationDetails.InsertOnSubmit(quotationdetail);
        }

        #endregion

        #region Subscription

        /// <summary>
        /// Helper method to return all subscriptions
        /// </summary>
        /// <returns>List with orderstatus objects</returns>
        public List<Subscription> GetSubscription()
        {
            return this.Subscriptions.ToList();
        }

        /// <summary>
        /// Helper method that returns subscription for a specific Subscription id
        /// </summary>
        /// <param name="SubscriptionId">Id for subscription</param>
        /// <returns>Subscription object</returns>
        public Subscription GetSubscription(int SubscriptionId)
        {
            return this.Subscriptions.SingleOrDefault(p => p.SubscriptionId == SubscriptionId);
        }

        /// <summary>
        /// Helper method that returns subscription description for a specific Subscription id
        /// </summary>
        /// <param name="SubscriptionId">Id for subscription</param>
        /// <returns>String with subscription description</returns>
        public string GetSubscriptionDescription(int SubscriptionId)
        {
            return this.Subscriptions.SingleOrDefault(p => p.SubscriptionId == SubscriptionId).Description;
        }

        /// <summary>
        /// Helper method that returns economic product number for subscription
        /// </summary>
        /// <param name="SubscriptionId">Id for subscription</param>
        /// <returns>Integer with economic product number</returns>
        public int? GetSubscriptionEconomicProductNumber(int SubscriptionId)
        {
            return this.Subscriptions.SingleOrDefault(p => p.SubscriptionId == SubscriptionId).EconomicProductNumber;
        }

        /// <summary>
        /// Helper method that returns subscription price for a specific Subscription id
        /// </summary>
        /// <param name="SubscriptionId">Id for subscription</param>
        /// <returns>Decimal with subscription price</returns>
        public decimal GetSubscriptionPrice(int SubscriptionId)
        {
            return this.Subscriptions.SingleOrDefault(p => p.SubscriptionId == SubscriptionId).StandardPrice;
        }

        /// <summary>
        /// Helper method that returns cars included in subscription for a specific Subscription id
        /// </summary>
        /// <param name="SubscriptionId">Id for subscription</param>
        /// <returns>Integer with cars included in subscription</returns>
        public int GetSubscriptionCarCount(int SubscriptionId)
        {
            return this.Subscriptions.SingleOrDefault(p => p.SubscriptionId == SubscriptionId).CarsIncluded;
        }

        /// <summary>
        /// Helper function that returns true/false if specific subscription allows car adding
        /// </summary>
        /// <param name="SubscriptionId">Id on subscription</param>
        /// <returns>True/False value</returns>
        public bool ValidateSubscriptionAllowsCars(int SubscriptionId)
        {
            return this.Subscriptions.SingleOrDefault(p => p.SubscriptionId == SubscriptionId).CarSubscription;
        }

        #endregion

        #region SubscriptionServiceTasklist

            #region SubscriptionServiceTasklistView

            /// <summary>
            /// Helper method to return list with all servicetasks including details for service task for a specific subscription
            /// </summary>
            /// <param name="SubscriptionId">Id on subscription</param>
            /// <returns>List with SubscriptionServiceTasklistView Objects</returns>
            public List<SubscriptionServiceTasklistView> GetSubScriptionServiceTasklistView(int SubscriptionId)
            {
                return this.SubscriptionServiceTasklistViews.Where(p => p.SubscriptionId == SubscriptionId).ToList();
            }

            #endregion

        #endregion

        #region CarStatus

        /// <summary>
        /// Helper method to return all car status
        /// </summary>
        /// <returns>List with carstatus objects</returns>
        public List<CarStatus> GetCarStatus()
        {
            return this.CarStatus.ToList();
        }

        /// <summary>
        /// Helper method that returns carstatus description for a specific carstatus id
        /// </summary>
        /// <param name="CarStatusId">Id for carstatus</param>
        /// <returns>String with carstatus description</returns>
        public string GetCarStatusDescription(int CarStatusId)
        {
            return this.CarStatus.SingleOrDefault(p => p.CarStatusId == CarStatusId).Description;
        }

        #endregion

        #region TireSet

        /// <summary>
        /// Helper method to return list with all tireset for a specific car
        /// </summary>
        /// <param name="CarId">Id on car</param>
        /// <returns>List with TireSet objects</returns>
        public List<TireSet> GetTireSets(int CarId)
        {
            return this.TireSets.Where(p => p.CarId == CarId).ToList();
        }

        /// <summary>
        /// Helper method to return tiresets with a specific status for a specific car
        /// </summary>
        /// <param name="CarId">Id on Car</param>
        /// <param name="TireSetStatus">Array list with TireSetStatus</param>
        /// <returns>List with TireSet objects</returns>
        public List<TireSet> GetCarTireSetByTireSetStatus(int CarId, int[] TireSetStatus)
        {
            return this.TireSets.Where(p => p.CarId == CarId).Where(d => TireSetStatus.Contains(d.TireSetStatusId)).ToList();
        }

        /// <summary>
        /// Helper method to return list with all tiresets for a specific customers
        /// </summary>
        /// <param name="CustomerId">Id on customers</param>
        /// <returns>List with TireSet objects</returns>
        public List<TireSet> GetTireSetsForCustomer(int CustomerId)
        {
            var tiresets =  from p in TireSets
                            join v in Cars on p.CarId equals v.CarId into pv
                            from x in pv.DefaultIfEmpty()
                            where x.CustomerId == CustomerId
                            select p;

            return tiresets.ToList();
        }

        public BusinessTireSetListView GetTireSetOnStockByCarRegistrationNumber(string RegistrationNumber)
        {
            return this.BusinessTireSetListViews.SingleOrDefault(p => p.RegistrationNumber == RegistrationNumber & p.ContainerCargoRoomId != null);
        }

        /// <summary>
        /// Helper method to return all tiresets for a specific customer - show business view
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <param name="IncludeChildCustomers">True/false value if child customer tiresets should be included</param>
        /// <returns>List with BusinessTireSetListView objects</returns>
        public List<BusinessTireSetListView> GetCustomerTireSets(int CustomerId, bool IncludeChildCustomers)
        {
            if (IncludeChildCustomers)
            {
                return this.BusinessTireSetListViews.Where(p => p.CustomerId == CustomerId | p.ParentCustomerId == CustomerId).ToList();
            }
            else
            {
                return this.BusinessTireSetListViews.Where(p => p.CustomerId == CustomerId).ToList();
            }
        }

        /// <summary>
        /// Helper method to return TireSet object
        /// </summary>
        /// <param name="TireSetId">Id on tireset</param>
        /// <returns>TireSet Object</returns>
        public TireSet GetTireSet(int TireSetId)
        {
            return this.TireSets.SingleOrDefault(p => p.TireSetId == TireSetId);
        }

        /// <summary>
        /// Helper method to return TireSet object
        /// </summary>
        /// <param name="TireSetId">Id on tireset</param>
        /// <param name="CarId">Id on car</param>
        /// <returns>TireSet Object</returns>
        public TireSet GetTireSet(int TireSetId, int CarId)
        {
            return this.TireSets.SingleOrDefault(p => p.CarId == CarId & p.TireSetId == TireSetId);
        }

        /// <summary>
        /// Helper method to return TireSet object for a specific car with a specific tireset status
        /// </summary>
        /// <param name="CarId">Id on car</param>
        /// <param name="TireSetStatusId">Id on tireset status</param>
        /// <returns>TireSet Object</returns>
        public TireSet GetCarTireSet(int CarId, int TireSetStatusId)
        {
            return this.TireSets.SingleOrDefault(p => p.CarId == CarId & p.TireSetStatusId == TireSetStatusId);
        }



        /// <summary>
        /// Helper methode to return number of tiresets for a specific car with a specific status
        /// </summary>
        /// <param name="CarId">Id on car</param>
        /// <param name="TireSetStatusId">Id on tireset status</param>
        /// <returns>Integer with number of tiresets</returns>
        public int CountTireSetWithStatus(int CarId, int TireSetStatusId)
        {
            return this.TireSets.Where(p => p.CarId == CarId & p.TireSetStatusId == TireSetStatusId).Count();
        }

        /// <summary>
        /// Helper function that adds a new TireSet to the database
        /// </summary>
        /// <param name="tireset">TireSet object</param>
        public void AddTireSet(TireSet tireset)
        {
            TireSets.InsertOnSubmit(tireset);
        }

            #region CarTireSetView

            // TODO: Overvej, at udfas CarTireSetView til fordel for BusinessTireSetListView
            // Udkommenteret d. 19 Juli 2012 - tjek for efterfølgende fejl ifht. dette
            // NOTE: Fandt ud af d. 20 juli 2012 at funktion GetCustomerCarTireSetViews bliver anvendt i ucCreateTask.ascx

        /// <summary>
        /// Helper method to return a specific BusinessTireSetListView
        /// </summary>
        /// <param name="TireSetId">Id on TireSet</param>
        /// <returns>BusinessTireSetListView object</returns>
        public BusinessTireSetListView GetTireSetView(int TireSetId)
        {
            return this.BusinessTireSetListViews.SingleOrDefault(p => p.TireSetId == TireSetId);
        }

        /// <summary>
        /// Helper method to return a specific CarTireSetView
        /// </summary>
        /// <param name="TireSetId">Id on TireSet</param>
        /// <returns>CarTireSetView object</returns>
        public CarTireSetView GetCarTireSetView(int TireSetId)
        {
            return this.CarTireSetViews.SingleOrDefault(p => p.TireSetId == TireSetId);
        }

        /// <summary>
        /// Helper method to return all Tiresets including car information
        /// </summary>
        /// <returns>List with CarTireSetView objects</returns>
        public List<CarTireSetView> GetCarTireSetViews()
        {
            return this.CarTireSetViews.ToList();
        }

        /// <summary>
        /// Helper method to return all Tiresets including car information for a specific customer 
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <returns>List with CarTireSetView objects</returns>
        public List<CarTireSetView> GetCarTireSetViews(int CustomerId)
        {
            return this.CarTireSetViews.Where(p => p.CustomerId == CustomerId).ToList();
        }

        /// <summary>
        /// Helper method to return all Tiresets including car information for a specific car
        /// </summary>
        /// <param name="CarId">Id on car</param>
        /// <returns>List with CarTireSetView objects</returns>
        public List<CarTireSetView> GetCustomerCarTireSetViews(int CarId)
        {
            return this.CarTireSetViews.Where(p => p.CarId == CarId).ToList();
        }

            #endregion

        #endregion

        #region TireSetLog

        /// <summary>
        /// Helper method to return all TireSetLogs for a specific TireSet 
        /// </summary>
        /// <param name="TireSetId">Id on TireSet</param>
        /// <returns>List with TireSetLog objects</returns>
        public List<TireSetLog> GetTireSetLog(int TireSetId)
        {
            return this.TireSetLogs.Where(p => p.TireSetId == TireSetId).ToList();
        }

        /// <summary>
        /// Helper function that adds a new TireSetLog to the database
        /// </summary>
        /// <param name="tiresetlog">TireSetLog object</param>
        public void AddTireSetLog(TireSetLog tiresetlog)
        {
            TireSetLogs.InsertOnSubmit(tiresetlog);
        }

        #endregion

        #region TireSetStatus

        /// <summary>
        /// Helper method to return all tireset status
        /// </summary>
        /// <returns>List with tiresetstatus objects</returns>
        public List<TireSetStatus> GetTireSetStatus()
        {
            return this.TireSetStatus.ToList();
        }

        /// <summary>
        /// Helper method to return specific tireset status
        /// </summary>
        /// <returns>TireSetStatus object</returns>
        public TireSetStatus GetTireSetStatus(int TireSetStatusId)
        {
            return this.TireSetStatus.SingleOrDefault(p => p.TireSetStatusId == TireSetStatusId);
        }

        /// <summary>
        /// Helper method that returns tireset description for a specific tiresetstatus id
        /// </summary>
        /// <param name="TireSetStatusId">Id for tiresetup status</param>
        /// <returns>String with tiresetstatus description</returns>
        public string GetTireSetStatusDescription(int TireSetStatusId)
        {
            return this.TireSetStatus.SingleOrDefault(p => p.TireSetStatusId == TireSetStatusId).Description;
        }

        #endregion

        #region TireType

        /// <summary>
        /// Helper method to return all tiretypes
        /// </summary>
        /// <returns>List with TireType objects</returns>
        public List<TireType> GetTireType()
        {
            return this.TireTypes.ToList();
        }

        /// <summary>
        /// Helper method that returns tiretype description for a specific tiretype id
        /// </summary>
        /// <param name="TireTypeId">Id for tiretypes description</param>
        /// <returns>String with tiretype description</returns>
        public string GetTireTypeDescription(int TireTypeId)
        {
            return this.TireTypes.SingleOrDefault(p => p.TireTypeId == TireTypeId).Description;
        }

        #endregion

        #region TireSpeedIndex

        /// <summary>
        /// Helper method to return all tirespeedindexes
        /// </summary>
        /// <returns>List with TireSpeedIndex objects</returns>
        public List<TireSpeedIndex> GetTireSpeedIndex()
        {
            return this.TireSpeedIndexes.ToList();
        }

        /// <summary>
        /// Helper method that returns tirespeedindex description for a specific tirespeedindex id
        /// </summary>
        /// <param name="TireSpeedIndexId">Id for tiretypes description</param>
        /// <returns>String with TireSpeedIndex description</returns>
        public string GetTireSpeedIndexDescription(int TireSpeedIndexId)
        {
            return this.TireSpeedIndexes.SingleOrDefault(p => p.TireSpeedIndexId == TireSpeedIndexId).Description;
        }

        /// <summary>
        /// Helper method that returns tirespeedindex id based on short description
        /// </summary>
        /// <param name="ShortDescription">String with short description for tirespeedindex</param>
        /// <returns>Integer with TireSpeedIndex Id</returns>
        public int GetTireSpeedIndexId(string ShortDescription)
        {
            return this.TireSpeedIndexes.SingleOrDefault(p => p.ShortDescription.Contains(ShortDescription)).TireSpeedIndexId;
        }

        #endregion

        #region TireLoadIndex

        /// <summary>
        /// Helper method to return all TireLoadIndexes
        /// </summary>
        /// <returns>List with TireLoadIndex objects</returns>
        public List<TireLoadIndex> GetTireLoadIndex()
        {
            return this.TireLoadIndexes.ToList();
        }

        /// <summary>
        /// Helper method that returns TireLoadIndex description for a specific TireLoadIndex id
        /// </summary>
        /// <param name="TireLoadIndexId">Id for tireloadindex description</param>
        /// <returns>String with TireLoadIndex description</returns>
        public string GetTireLoadIndexDescription(int TireLoadIndexId)
        {
            return this.TireLoadIndexes.SingleOrDefault(p => p.TireLoadIndexId == TireLoadIndexId).Description;
        }

        /// <summary>
        /// Helper method that returns tireloadindex id based on description
        /// </summary>
        /// <param name="LoadIndex">String with description for loadindex</param>
        /// <returns>Integer with TireLoadIndex Id</returns>
        public int GetTireLoadIndexId(string LoadIndex)
        {
            return this.TireLoadIndexes.SingleOrDefault(p => p.Description.Trim() == LoadIndex).TireLoadIndexId;
        }

        #endregion

        #region TireBrand

        /// <summary>
        /// Helper method to return all TireBrands
        /// </summary>
        /// <returns>List with TireBrand objects</returns>
        public List<TireBrand> GetTireBrands()
        {
            return this.TireBrands.ToList();
        }

        /// <summary>
        /// Helper method that returns TireBrand description for a specific TireBrand id
        /// </summary>
        /// <param name="TireBrandId">Id for tiretypes description</param>
        /// <returns>String with TireBrand description</returns>
        public string GetTireBrandDescription(int TireBrandId)
        {
            return this.TireBrands.SingleOrDefault(p => p.TireBrandId == TireBrandId).Description;
        }

        /// <summary>
        /// Helper method that returns tirebrand id based on short description
        /// </summary>
        /// <param name="ShortDescription">String with short description for tirebrand</param>
        /// <returns>Integer with TireBrandId</returns>
        public int GetTireBrandId(string ShortDescription)
        {
            return this.TireBrands.SingleOrDefault(p => p.ShortDescription == ShortDescription).TireBrandId;
        }

        #endregion

        #region Car

        /// <summary>
        /// Helper method to return all cars
        /// </summary>
        /// <returns>List with Car objects</returns>
        public List<Car> GetCars()
        {
            return this.Cars.ToList();
        }

        /// <summary>
        /// Helper method to return all cars with a specific registration number
        /// </summary>
        /// <param name="RegNumber">String with registration number</param>
        /// <returns>List with Car objects</returns>
        public List<Car> GetCarsByRegistrationNumber(string RegNumber)
        {
            return this.Cars.Where(p => p.RegistrationNumber == RegNumber).ToList();
        }

        /// <summary>
        /// Helper method to return all cars for a specific customer (OBS!!! also underlying department/customer cars) - show business view
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <returns>List with BusinessCarListView objects</returns>
        public List<BusinessCarListView> GetCustomerCarsAll(int CustomerId, bool IncludeChildCustomers)
        {
            if (IncludeChildCustomers)
            {
                return this.BusinessCarListViews.Where(p => p.CustomerId == CustomerId | p.ParentCustomerId == CustomerId).ToList();
            }
            else
            {
                return this.BusinessCarListViews.Where(p => p.CustomerId == CustomerId).ToList();
            }
        }

        /// <summary>
        /// Helper method to return all cars for a specific customer - show business view
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <returns>List with BusinessCarListView objects</returns>
        public List<BusinessCarListView> GetCustomerCars(int CustomerId)
        {
            return this.BusinessCarListViews.Where(p => p.CustomerId == CustomerId).ToList();
        }

        /// <summary>
        /// Helper method to return list with all cars for a specific customers
        /// </summary>
        /// <param name="CustomerId">Id on customers</param>
        /// <returns>List with Car objects</returns>
        public List<Car> GetCarsByCarStatusId(int CarStatusId)
        {
            return this.Cars.Where(p => p.CarStatusId == CarStatusId).ToList();
        }

        /// <summary>
        /// Helper method to return list with all cars with a specific carstatus
        /// </summary>
        /// <param name="CarStatusId">Id on Carstatus</param>
        /// <returns>List with Car objects</returns>
        public List<Car> GetCars(int CustomerId)
        {
            return this.Cars.Where(p => p.CustomerId == CustomerId).ToList();
        }

        /// <summary>
        /// Helper method to return Car object
        /// </summary>
        /// <param name="CarId">Id on car</param>
        /// <returns>Car Object</returns>
        public Car GetCar(int CarId)
        {
            return this.Cars.SingleOrDefault(p => p.CarId == CarId);
        }

        /// <summary>
        /// Helper method to return Car object
        /// </summary>
        /// <param name="CarId">Id on car</param>
        /// <param name="CustomerId">Id on customers</param>
        /// <returns>Car Object</returns>
        public Car GetCar(int CarId, int CustomerId)
        {
            return this.Cars.SingleOrDefault(p => p.CarId == CarId & p.CustomerId == CustomerId);
        }

        /// <summary>
        /// Helper method to return Car registration number
        /// </summary>
        /// <param name="CarId">Id on car</param>
        /// <returns>Car Object</returns>
        public string GetCarRegistrationNumber(int CarId)
        {
            return this.Cars.SingleOrDefault(p => p.CarId == CarId).RegistrationNumber;
        }

        /// <summary>
        /// Helper method to return list with all cars related to a specific customer subscription
        /// </summary>
        /// <param name="CustomerSubscriptionId">Id on customersubscription</param>
        /// <returns>List with Car objects</returns>
        public List<Car> GetCarByCustomerSubscriptionId(int CustomerSubscriptionId)
        {
            return this.Cars.Where(p => p.CustomerSubscriptionId == CustomerSubscriptionId).ToList();
        }

        ///// <summary>
        ///// Helper method to return number of cars assigned to a specific subscription for a specific customer
        ///// </summary>
        ///// <param name="SubscriptionId">Id on subscription</param>
        ///// <param name="CustomerId">Id on Customers</param>
        ///// <returns>Integer with number of cars assigned to subscription</returns>
        //public int CountCarsWithSubscription(int SubscriptionId, int CustomerId)
        //{
        //    return this.Cars.Where(p => p.CustomerId == CustomerId & p.SubscriptionId == SubscriptionId).Count();
        //}

        /// <summary>
        /// Helper function that adds a new Car to the database
        /// </summary>
        /// <param name="car">Car object</param>
        public void AddCar(Car car)
        {
            Cars.InsertOnSubmit(car);
        }


        #endregion

        #region CarBrand 

        /// <summary>
        /// Helper method to return all car brand objects
        /// </summary>
        /// <returns>List with CarBrand objects</returns>
        public List<CarBrand> GetCarBrand()
        {
            return this.CarBrands.ToList();
        }

        /// <summary>
        /// Helper method that returns car brand description for a specific carbrandid
        /// </summary>
        /// <param name="CarBrandId">Id for car brand</param>
        /// <returns>String with carbrand description</returns>
        public string GetCarBrandDescription(int CarBrandId)
        {
            return this.CarBrands.SingleOrDefault(p => p.CarBrandId == CarBrandId).Description;
        }

        #endregion

        #region Container

        /// <summary>
        /// Helper method to return all containers
        /// </summary>
        /// <returns>List with Container objects</returns>
        public List<Container> GetContainers()
        {
            return this.Containers.ToList();
        }

        /// <summary>
        /// Helper method to return Container object
        /// </summary>
        /// <param name="ContainerId">Id on container</param>
        /// <returns>Container Object</returns>
        public Container GetContainer(int ContainerId)
        {
            return this.Containers.SingleOrDefault(p => p.ContainerId == ContainerId);
        }

        /// <summary>
        /// Helper method to return container number
        /// </summary>
        /// <param name="ContainerId">Id on container</param>
        /// <returns>String with container number</returns>
        public string GetContainerNumber(int ContainerId)
        {
            return this.Containers.SingleOrDefault(p => p.ContainerId == ContainerId).ContainerNumber;
        }

        /// <summary>
        /// Helper method to return container number for a specific cargoroom
        /// </summary>
        /// <param name="ContainerCargoRoomId">Id on Cargo Room</param>
        /// <returns>String with container number</returns>
        public string GetContainerNumberByCargoRoomId(int ContainerCargoRoomId) 
        {
            return this.ContainerCargoRoomTireSetViews.SingleOrDefault(p => p.ContainerCargoRoomId == ContainerCargoRoomId).ContainerNumber;
        }

        /// <summary>
        /// Helper method to return Container object
        /// </summary>
        /// <param name="ContainerNumber">Number on container</param>
        /// <returns>Container Object</returns>
        public Container GetContainer(string ContainerNumber)
        {
            return this.Containers.SingleOrDefault(p => p.ContainerNumber == ContainerNumber);
        }

        /// <summary>
        /// Helper method to return number of containers within a specific zipcode
        /// </summary>
        /// <param name="ZipCode">ZipCode area</param>
        /// <returns>Integer with count</returns>
        public int CountContainerWithZipCode(int ZipCode)
        {
            return this.Containers.Where(p => p.ZipCode == ZipCode).Count();
        }

        /// <summary>
        /// Helper function that adds a new Container to the database
        /// </summary>
        /// <param name="container">Container object</param>
        public void AddContainer(Container container)
        {
            Containers.InsertOnSubmit(container);
        }

        #endregion

        #region ContainerServiceCarCoverage

        public List<ContainerServiceCarCoverage> GetContainerServiceCarCoverageByServiceCarId(int ServiceCarId)
        {
            return this.ContainerServiceCarCoverages.Where(p => p.ServiceCarId == ServiceCarId).ToList();
        }

        public ContainerServiceCarCoverage GetContainerServiceCarCoverage(int ServiceCarId, int ContainerId)
        {
            return this.ContainerServiceCarCoverages.SingleOrDefault(p => p.ServiceCarId == ServiceCarId & p.ContainerId == ContainerId);
        }

        /// <summary>
        /// Helper method to return all service cars for a specific container
        /// </summary>
        /// <returns>List with ContainerServiceCarCoverage objects</returns>
        public int[] GetContainerServiceCars(int ContainerId)
        {
            List<ContainerServiceCarCoverage> servicecars = this.ContainerServiceCarCoverages.Where(p => p.ContainerId == ContainerId).ToList();

            int[] ids = new int[servicecars.Count];
            int i = 0;

            if (servicecars != null)
            {
                foreach (ContainerServiceCarCoverage servicecar in servicecars)
                {
                    ids[i] = servicecar.ServiceCarId;
                    i += 1;
                }
            }

            return ids;
        }

        /// <summary>
        /// Helper method to return all containers for a specific servicecar
        /// </summary>
        /// <returns>List with ContainerServiceCarCoverage objects</returns>
        public int[] GetServiceCarContainers(int ServiceCarId)
        {
            List<ContainerServiceCarCoverage> containers = this.ContainerServiceCarCoverages.Where(p => p.ServiceCarId == ServiceCarId).ToList();

            int[] ids = new int[containers.Count];
            int i = 0;

            if (containers != null)
            {
                foreach (ContainerServiceCarCoverage container in containers)
                {
                    ids[i] = container.ContainerId;
                    i += 1;
                }
            }

            return ids;
        }

        /// <summary>
        /// Helper function that adds a new ContainerServiceCarCoverage to the database
        /// </summary>
        /// <param name="coverage">ContainerServiceCarCoverage object</param>
        public void AddContainerServiceCarCoverage(ContainerServiceCarCoverage coverage)
        {
            ContainerServiceCarCoverages.InsertOnSubmit(coverage);
        }

        #endregion

        #region ContainerCargoRooms

        /// <summary>
        /// Helper method to return all ContainerCargoRoom
        /// </summary>
        /// <returns>List with ContainerCargoRoom objects</returns>
        public List<ContainerCargoRoom> GetContainerCargoRooms()
        {
            return this.ContainerCargoRooms.ToList();
        }

        /// <summary>
        /// Helper method to return all ContainerCargoRoom for a specific Container
        /// </summary>
        /// <param name="ContainerId">Id on Container</param>
        /// <returns>List with ContainerCargoRoom objects</returns>
        public List<ContainerCargoRoom> GetContainerCargoRooms(int ContainerId)
        {
            return this.ContainerCargoRooms.Where(p => p.ContainerId == ContainerId).ToList();
        }

        /// <summary>
        /// Helper method to return ContainerCargoRoom object
        /// </summary>
        /// <param name="ContainerCargoRoomId">Id on ContainerCargoRoom</param>
        /// <returns>ContainerCargoRoom Object</returns>
        public ContainerCargoRoom GetContainerCargoRoom(int ContainerCargoRoomId)
        {
            return this.ContainerCargoRooms.SingleOrDefault(p => p.ContainerCargoRoomId == ContainerCargoRoomId);
        }

        /// <summary>
        /// Helper method to return ContainerCargoRoom number
        /// </summary>
        /// <param name="ContainerCargoRoomId">Id on ContainerCargoRoom</param>
        /// <returns>Integer with cargo room number</returns>
        public int GetContainerCargoRoomNumber(int ContainerCargoRoomId)
        {
            return this.ContainerCargoRooms.SingleOrDefault(p => p.ContainerCargoRoomId == ContainerCargoRoomId).CargoRoomNumber;
        }

        /// <summary>
        /// Helper method to return ContainerId for a specific ContainerCargoRoom object
        /// </summary>
        /// <param name="ContainerCargoRoomId">Id on ContainerCargoRoom</param>
        /// <returns>Integer with Container Id</returns>
        public int GetContainerIdForCargoRoom(int ContainerCargoRoomId)
        {
            return this.ContainerCargoRooms.SingleOrDefault(p => p.ContainerCargoRoomId == ContainerCargoRoomId).ContainerId;
        }

        /// <summary>
        /// Helper method to return ContainerCargoRoomId for a specific cargoroomnumber in a specific containerid
        /// </summary>
        /// <param name="ContainerId">Id on container</param>
        /// <param name="CargoRoomNumber">Cargo room number</param>
        /// <returns></returns>
        public int GetContainerCargoRoomIdByContainerIdAndCargoRoomNumber(int ContainerId, int CargoRoomNumber) 
        {
            return this.ContainerCargoRooms.SingleOrDefault(p => p.ContainerId == ContainerId & p.CargoRoomNumber == CargoRoomNumber).ContainerCargoRoomId;
        }

        /// <summary>
        /// Helper method to return last cargo number for a specific containerCargoRooms
        /// </summary>
        /// <param name="ContainerId">Id on container</param>
        /// <returns>Integer with cargo room number</returns>
        public int GetLastContainerCargoRoomNumber(int ContainerId)
        {
            try
            {
                return this.ContainerCargoRooms.Where(p => p.ContainerId == ContainerId).OrderByDescending(p => p.CargoRoomNumber).First().CargoRoomNumber;
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        /// <summary>
        /// Helper function that adds a new ContainerCargoRoom to the database
        /// </summary>
        /// <param name="cargoroom">ContainerCargoRoom object</param>
        public void AddContainerCargoRoom(ContainerCargoRoom cargoroom)
        {
            ContainerCargoRooms.InsertOnSubmit(cargoroom);
        }

            #region ContainerCargoRoomTireSetView

            /// <summary>
            /// Helper method to return a specific CarTireSetView
            /// </summary>
            /// <param name="ContainerCargoRoomId">Id on ContainerCargoRoom</param>
            /// <returns>ContainerCargoRoomTireSetView object</returns>
            public ContainerCargoRoomTireSetView GetContainerCargoRoomTireSetView(int ContainerCargoRoomId)
            {
                return this.ContainerCargoRoomTireSetViews.SingleOrDefault(p => p.ContainerCargoRoomId == ContainerCargoRoomId);
            }

            /// <summary>
            /// Helper method to return all ContainerCargoRooms including tireset, car, customer and subscription id information
            /// </summary>
            /// <returns>List with ContainerCargoRoomTireSetView objects</returns>
            public List<ContainerCargoRoomTireSetView> GetContainerCargoRoomTireSetViews()
            {
                return this.ContainerCargoRoomTireSetViews.ToList();
            }

            /// <summary>
            /// Helper method to return all ContainerCargoRooms including tireset, car, customer and subscription id information for a specific container
            /// </summary>
            /// <param name="ContainerId">Id on container</param>
            /// <returns>List with ContainerCargoRoomTireSetView objects</returns>
            public List<ContainerCargoRoomTireSetView> GetContainerCargoRoomTireSetViews(int ContainerId)
            {
                return this.ContainerCargoRoomTireSetViews.Where(p => p.ContainerId == ContainerId).ToList();
            }

            /// <summary>
            /// Helper method to return list with all free containerCargoRooms with option for adding another cargoroom (free or not free)
            /// </summary>
            /// <param name="ContainerCargoRoomId">Id on cargo room to add (Optional)</param>
            /// <returns>List with ContainerCargoRoomTireSetView objects</returns>
            public List<ContainerCargoRoomTireSetView> GetFreeContainerCargoRooms(int? ContainerCargoRoomId)
            {
                List<ContainerCargoRoomTireSetView> list = this.ContainerCargoRoomTireSetViews.Where(p => p.TireSetId == null).ToList();

                if (ContainerCargoRoomId != null)
                {
                    ContainerCargoRoomTireSetView addroom = this.GetContainerCargoRoomTireSetView(Convert.ToInt32(ContainerCargoRoomId));

                    if (addroom != null)
                    {
                        list.Add(addroom);
                    }
                }

                return list;
            }

            #endregion

        #endregion

        #region ServiceCar

        /// <summary>
        /// Helper method to return all service cars
        /// </summary>
        /// <returns>List with ServiceCar objects</returns>
        public List<ServiceCar> GetServiceCars()
        {
            return this.ServiceCars.ToList();
        }

        /// <summary>
        /// Helper method to return ServiceCar object
        /// </summary>
        /// <param name="ServiceCarId">Id on servicecar</param>
        /// <returns>ServiceCar Object</returns>
        public ServiceCar GetServiceCar(int ServiceCarId)
        {
            return this.ServiceCars.SingleOrDefault(p => p.ServiceCarId == ServiceCarId);
        }


        ///// <summary>
        ///// Helper method to return number of containers within a specific zipcode
        ///// </summary>
        ///// <param name="ZipCode">ZipCode area</param>
        ///// <returns>Integer with count</returns>
        //public int CounContainerWithZipCode(int ZipCode)
        //{
        //    return this.Containers.Where(p => p.ZipCode == ZipCode).Count();
        //}

        /// <summary>
        /// Helper function that adds a new ServiceCar to the database
        /// </summary>
        /// <param name="servicecar">Container object</param>
        public void AddServiceCar(ServiceCar servicecar)
        {
            ServiceCars.InsertOnSubmit(servicecar);
        }

        #endregion

        #region ServiceCarSchedule

        /// <summary>
        /// Helper method to return all servicecars schedules
        /// </summary>
        /// <returns>List with ServiceCarSchedule objects</returns>
        public List<ServiceCarSchedule> GetServiceCarSchedules()
        {
            return this.ServiceCarSchedules.ToList();
        }

        /// <summary>
        /// Helper method to return all servicecars schedules
        /// </summary>
        /// <param name="EmployeeId">Id on employees</param>
        /// <param name="ServiceCarId">Id on servicecar</param>
        /// <param name="AfterScheduleDate">The date where the scheduledate should be placed after</param>
        /// <returns>List with ServiceCarSchedule objects</returns>
        public List<ServiceCarSchedule> GetServiceCarSchedules(int ServiceCarId, DateTime AfterScheduleDate)
        {
            return this.ServiceCarSchedules.Where(p => p.ServiceCarId == ServiceCarId & p.ScheduleDate > AfterScheduleDate).OrderBy(p => p.ScheduleDate).ToList();
        }

        /// <summary>
        /// Helper method to return all servicecars schedules
        /// </summary>
        /// <param name="EmployeeId">Id on employees</param>
        /// <param name="ServiceCars">Array with servicecars (ServiceCarId)</param>
        /// <param name="AfterScheduleDate">The date where the scheduledate should be placed after</param>
        /// <returns>List with ServiceCarSchedule objects</returns>
        public List<ServiceCarSchedule> GetServiceCarSchedules(int[] ServiceCars, DateTime AfterScheduleDate)
        {
            return this.ServiceCarSchedules.Where(p => p.ScheduleDate > AfterScheduleDate & ServiceCars.Contains(p.ServiceCarId)).OrderBy(p => p.ScheduleDate).ToList();
        }

        public bool ScheduleDateExists(int ServiceCarId, DateTime ScheduleDate)
        {
            ServiceCarSchedule carschedule = this.ServiceCarSchedules.SingleOrDefault(p => p.ServiceCarId == ServiceCarId & p.ScheduleDate == ScheduleDate);

            if (carschedule == null)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        public bool ScheduleDateExistsOnContainer(int ContainerId, DateTime ScheduleDate)
        {
            List<ServiceCarSchedulePackingView> carschedule = this.ServiceCarSchedulePackingViews.Where(p => p.ContainerId == ContainerId & p.ScheduleDate == ScheduleDate).ToList();

            if (carschedule.Count == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }

        /// <summary>
        /// Helper method to return ServiceCarSchedule object
        /// </summary>
        /// <param name="ServiceCarScheduleId">Id on servicecar</param>
        /// <returns>ServiceCarSchedule Object</returns>
        public ServiceCarSchedule GetServiceCarSchedule(int ServiceCarScheduleId)
        {
            return this.ServiceCarSchedules.SingleOrDefault(p => p.ServiceCarScheduleId == ServiceCarScheduleId);
        }

        /// <summary>
        /// Helper method to return ServiceCarSchedule object
        /// </summary>
        /// <param name="ServiceCarId">Id on servicecar</param>
        /// <param name="ScheduleDate">Date</param>
        /// <returns>ServiceCarSchedule Object</returns>
        public ServiceCarSchedule GetServiceCarSchedule(int ServiceCarId, DateTime ScheduleDate)
        {
            return this.ServiceCarSchedules.SingleOrDefault(p => p.ServiceCarId == ServiceCarId & p.ScheduleDate == ScheduleDate);
        }

        /// <summary>
        /// Helper method to return true/false value if ServiceCarSchedule is a group booking object
        /// </summary>
        /// <param name="ServiceCarId">Id on servicecar</param>
        /// <param name="ScheduleDate">Date</param>
        /// <returns>ServiceCarSchedule Object</returns>
        public bool IsServiceCarScheduleGroupBooking(int ServiceCarId, DateTime ScheduleDate)
        {
            bool value = false;

            if (this.ServiceCarSchedules.SingleOrDefault(p => p.ServiceCarId == ServiceCarId & p.ScheduleDate == ScheduleDate).GroupServiceCarScheduleId != null)
            {
                value = true;
            }

            return value;
        }

        /// <summary>
        /// Helper method to return all ServiceCarSchedule objects assigned to a specific GroupServiceCarSchedule
        /// </summary>
        /// <param name="GroupServiceCarScheduleId">Id on GroupServiceCarSchedule</param>
        /// <returns>List with ServiceCarSchedule objects</returns>
        public List<ServiceCarSchedule> GetServiceCarSchedulesByGroupServiceCarScheduleId(int GroupServiceCarScheduleId)
        {
            return this.ServiceCarSchedules.Where(p => p.GroupServiceCarScheduleId == GroupServiceCarScheduleId).ToList();
        }

        /// <summary>
        /// Helper function that adds a new ServiceCarSchedule to the database
        /// </summary>
        /// <param name="servicecarsschedule">ServiceCarSchedule object</param>
        public void AddServiceCarSchedule(ServiceCarSchedule servicecarsschedule)
        {
            ServiceCarSchedules.InsertOnSubmit(servicecarsschedule);
        }

        #endregion

        #region ServiceCarScheduleDetail

        /// <summary>
        /// Helper method to return all servicecars schedule Details
        /// </summary>
        /// <returns>List with ServiceCarScheduleDetail objects</returns>
        public List<ServiceCarScheduleDetail> GetServiceCarScheduleDetails()
        {
            return this.ServiceCarScheduleDetails.ToList();
        }

        /// <summary>
        /// Helper method to return all servicedetails for a specific customer - show business view
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <param name="IncludeChildCustomers">True/false value to indicate if child customers should also be shown</param>
        /// <returns>List with BusinessServiceListView objects</returns>
        public List<BusinessServiceListView> GetCustomerService(int CustomerId, bool IncludeChildCustomers)
        {
            if (IncludeChildCustomers)
            {
                return this.BusinessServiceListViews.Where(p => p.CustomerId == CustomerId | p.ParentCustomerId == CustomerId).ToList();
            }
            else
            {
                return this.BusinessServiceListViews.Where(p => p.CustomerId == CustomerId).ToList();
            }
        }

        /// <summary>
        /// Helper method to return all servicedetails for a specific customer - show business view
        /// </summary>
        /// <param name="CarId">Id on car</param>
        /// <returns>List with BusinessServiceListView objects</returns>
        public List<BusinessServiceListView> GetCustomerCarService(int CarId)
        {
            return this.BusinessServiceListViews.Where(p => p.CarId == CarId).ToList();
        }

        /// <summary>
        /// Helper method to return ServiceCarScheduleDetail object
        /// </summary>
        /// <param name="ServiceCarScheduleDetailId">Id on servicecarscheduledetail</param>
        /// <returns>ServiceCarScheduleDetail Object</returns>
        public ServiceCarScheduleDetail GetServiceCarScheduleDetail(int ServiceCarScheduleDetailId)
        {
            return this.ServiceCarScheduleDetails.SingleOrDefault(p => p.ServiceCarScheduleDetailId == ServiceCarScheduleDetailId);
        }

        /// <summary>
        /// Helper method to return ServiceCarScheduleDetail object
        /// </summary>
        /// <param name="DetailGuid">Guid i on servicecarscheduledetail</param>
        /// <returns>ServiceCarScheduleDetail Object</returns>
        public ServiceCarScheduleDetail GetServiceCarScheduleDetail(Guid DetailGuid)
        {
            return this.ServiceCarScheduleDetails.SingleOrDefault(p => p.DetailGuid == DetailGuid);
        }

        /// <summary>
        /// Helper method to return ServiceCarScheduleDetail object
        /// </summary>
        /// <param name="TaskId">Id on task related to servicecarscheduledetail</param>
        /// <returns>ServiceCarScheduleDetail Object</returns>
        public ServiceCarScheduleDetail GetServiceCarScheduleDetailByTaskId(int TaskId)
        {
            return this.ServiceCarScheduleDetails.SingleOrDefault(p => p.TaskId == TaskId);
        }

        /// <summary>
        /// Helper method to return list with ServiceCarSchedule objects
        /// </summary>
        /// <param name="ServiceCarId">Id on servicecar</param>
        /// <param name="ScheduleDate">Date</param>
        /// <returns>List with ServiceCarScheduleDetail Object</returns>
        public List<ServiceCarScheduleDetail> GetServiceCarScheduleDetails(int ServiceCarId, DateTime ScheduleDate)
        {
            return this.ServiceCarScheduleDetails.Where(p => p.ServiceCarId == ServiceCarId & p.ScheduleDate == ScheduleDate).ToList();
        }

        /// <summary>
        /// Helper method to return list with ServiceCarSchedule objects for a specific scheduledate
        /// </summary>
        /// <param name="ScheduleDate">Date</param>
        /// <returns>List with ServiceCarScheduleDetail Object</returns>
        public List<ServiceCarScheduleDetail> GetServiceCarScheduleDetails(DateTime ScheduleDate)
        {
            return this.ServiceCarScheduleDetails.Where(p => p.ScheduleDate == ScheduleDate).ToList();
        }

        /// <summary>
        /// Helper method to return list with ServiceCarSchedule objects for a specific service car within a specific timeperiod
        /// </summary>
        /// <param name="ServiceCarId">Id on servicecar</param>
        /// <param name="FromScheduleDate">From specific date</param>
        /// <param name="ToScheduleDate">To specific date</param>
        /// <returns>List with ServiceCarScheduleDetail Object</returns>
        public List<ServiceCarScheduleDetail> GetServiceCarScheduleDetails(int ServiceCarId, DateTime FromScheduleDate, DateTime ToScheduleDate)
        {
            return this.ServiceCarScheduleDetails.Where(p => p.ServiceCarId == ServiceCarId & p.ScheduleDate >= FromScheduleDate & p.ScheduleDate <= ToScheduleDate).ToList();
        }

        /// <summary>
        /// Helper method to return sum with total distance for ServiceCarSchedule objects
        /// </summary>
        /// <param name="ServiceCarId">Id on servicecar</param>
        /// <param name="ScheduleDate">Date</param>
        /// <returns>Decimal with sum of distance</returns>
        public decimal SumServiceCarScheduleDetailsDistance(int ServiceCarId, DateTime ScheduleDate)
        {
            return (0 + this.ServiceCarScheduleDetails.Where(p => p.ServiceCarId == ServiceCarId & p.ScheduleDate == ScheduleDate).Sum(p => Convert.ToDecimal(p.DrivingDistance)));
        }

        /// <summary>
        /// Helper method to return sum with total drivingtime for ServiceCarSchedule objects
        /// </summary>
        /// <param name="ServiceCarId">Id on servicecar</param>
        /// <param name="ScheduleDate">Date</param>
        /// <returns>Decimal with sum of EstimatedDrivingTime</returns>
        public decimal SumServiceCarScheduleDetailsEstimatedDrivingTime(int ServiceCarId, DateTime ScheduleDate)
        {
            return (0 + this.ServiceCarScheduleDetails.Where(p => p.ServiceCarId == ServiceCarId & p.ScheduleDate == ScheduleDate).Sum(p => Convert.ToDecimal(p.EstimatedDrivingTime)));
        }

        /// <summary>
        /// Helper method that counts number of servicecarscheduledetails
        /// </summary>
        /// <param name="ServiceCarId">Id on servicecar</param>
        /// <param name="ScheduleDate">Date</param>
        /// <param name="ScheduleDetailTypes">Array with integers representing ScheduleDetailTypes</param>
        /// <param name="FromDate">Date from where the ServiceCarScheduleDetail must start/be larger than</param>
        /// <returns>Integer with count number</returns>
        public int CountScheduleDetailTypesInPlannedScheduleDetail(int ServiceCarId, DateTime ScheduleDate, int[] ScheduleDetailTypes, DateTime FromDate)
        {
            return this.ServiceCarScheduleDetails.Where(p => p.ServiceCarId == ServiceCarId & p.ScheduleDate == ScheduleDate & ScheduleDetailTypes.Contains(p.ScheduleDetailTypeId)).Count();
        }

        /// <summary>
        /// Helper function that adds a new ServiceCarScheduleDetail to the database
        /// </summary>
        /// <param name="servicecarsscheduledetail">ServiceCarScheduleDetail object</param>
        public void AddServiceCarScheduleDetail(ServiceCarScheduleDetail servicecarsscheduledetail)
        {
            ServiceCarScheduleDetails.InsertOnSubmit(servicecarsscheduledetail);
        }

        #endregion

        #region ServiceCarScheduleDetailRating

        /// <summary>
        /// Helper method to return rating for a specific scheduledetail
        /// </summary>
        /// <param name="ServiceCarScheduleDetailId">Id on scheduledetail</param>
        /// <returns>Integer with rating</returns>
        public int GetServiceCarScheduleDetailRating(int ServiceCarScheduleDetailId)
        {
            return this.ServiceCarScheduleDetailRatings.SingleOrDefault(p => p.ServiceCarScheduleDetailId == ServiceCarScheduleDetailId).Rating;
        }

        /// <summary>
        /// Helper method to return all ratings for a specific customer - show business view
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <returns>List with BusinessRatingListView objects</returns>
        public List<BusinessRatingListView> GetCustomerBusinessRatings(int CustomerId)
        {
            return this.BusinessRatingListViews.Where(p => p.CustomerId == CustomerId | p.ParentCustomerId == CustomerId).ToList();
        }

        /// <summary>
        /// Helper function that adds a new ServiceCarScheduleDetailRating to the database
        /// </summary>
        /// <param name="servicecarsscheduledetailrating">ServiceCarScheduleDetailRating object</param>
        public void AddServiceCarScheduleDetailRating(ServiceCarScheduleDetailRating servicecarsscheduledetailrating)
        {
            ServiceCarScheduleDetailRatings.InsertOnSubmit(servicecarsscheduledetailrating);
        }
        #endregion

        #region ServiceCarScheduleTasklist
        // NOTE: Outcommented by Jannik 19/1-2013

        ///// <summary>
        ///// Helper method to return all servicecars schedule tasklists
        ///// </summary>
        ///// <returns>List with ServiceCarScheduleTasklist objects</returns>
        //public List<ServiceCarScheduleTasklist> GetServiceCarScheduleTasklists()
        //{
        //    return this.ServiceCarScheduleTasklists.ToList();
        //}


        ///// <summary>
        ///// Helper method to return list with all servicetask for a specific schedule detail
        ///// </summary>
        ///// <param name="ServiceCarScheduleDetailId">Id on service schedule detail</param>
        ///// <returns>List with ServiceCarScheduleTasklist objects</returns>
        //public List<ServiceCarScheduleTasklist> GetServiceCarScheduleTasklists(int ServiceCarScheduleDetailId)
        //{
        //    return this.ServiceCarScheduleTasklists.Where(p => p.ServiceCarScheduleDetailId == ServiceCarScheduleDetailId).ToList();
        //}

        ///// <summary>
        ///// Helper method to return ServiceCarScheduleTasklist object
        ///// </summary>
        ///// <param name="ServiceCarScheduleTaskListId">Id on ServiceCarScheduleTasklist</param>
        ///// <returns>ServiceCarScheduleTasklist Object</returns>
        //public ServiceCarScheduleTasklist GetServiceCarScheduleTasklist(int ServiceCarScheduleTaskListId)
        //{
        //    return this.ServiceCarScheduleTasklists.SingleOrDefault(p => p.ServiceCarScheduleTaskListId == ServiceCarScheduleTaskListId);
        //}

        ///// <summary>
        ///// Helper function that adds a new ServiceCarScheduleTasklist to the database
        ///// </summary>
        ///// <param name="servicecarsscheduletasklist">ServiceCarScheduleTasklist object</param>
        //public void AddServiceCarScheduleTasklist(ServiceCarScheduleTasklist servicecarsscheduletasklist)
        //{
        //    ServiceCarScheduleTasklists.InsertOnSubmit(servicecarsscheduletasklist);
        //}

        #endregion


        #region GroupServiceCarSchedule

        /// <summary>
        /// Helper method to return all group servicecars schedules
        /// </summary>
        /// <returns>List with GroupServiceCarSchedules objects</returns>
        public List<GroupServiceCarSchedule> GetGroupServiceCarSchedules()
        {
            return this.GroupServiceCarSchedules.ToList();
        }

        /// <summary>
        /// Helper method to return all group servicecars schedules based on inactive or not
        /// </summary>
        /// <param name="InActive">True/False if group schedule is inactive or not</param>
        /// <returns>List with GroupServiceCarSchedule objects</returns>
        public List<GroupServiceCarSchedule> GetGroupServiceCarSchedules(bool InActive)
        {
            return this.GroupServiceCarSchedules.Where(p => p.InActive == InActive).ToList();
        }

        /// <summary>
        /// Helper method to return all group servicecars schedules for a specific customer
        /// </summary>
        /// <param name="CustomerId">Id on Customer</param>
        /// <returns>List with GroupServiceCarSchedule objects</returns>
        public List<GroupServiceCarSchedule> GetGroupServiceCarSchedules(int CustomerId)
        {
            return this.GroupServiceCarSchedules.Where(p => p.CustomerId == CustomerId).ToList();
        }

        /// <summary>
        /// Helper method to return GroupServiceCarSchedule object
        /// </summary>
        /// <param name="GroupServiceCarScheduleId">Id on group servicecarschedule</param>
        /// <returns>GroupServiceCarSchedule Object</returns>
        public GroupServiceCarSchedule GetGroupServiceCarSchedule(int GroupServiceCarScheduleId)
        {
            return this.GroupServiceCarSchedules.SingleOrDefault(p => p.GroupServiceCarScheduleId == GroupServiceCarScheduleId);
        }

        /// <summary>
        /// Helper method to return GroupServiceCarSchedule object
        /// </summary>
        /// <param name="GroupGuid">Id (unieque GUID) on group servicecarschedule</param>
        /// <returns>GroupServiceCarSchedule Object</returns>
        public GroupServiceCarSchedule GetGroupServiceCarSchedule(Guid GroupGuid)
        {
            return this.GroupServiceCarSchedules.SingleOrDefault(p => p.GroupGuid == GroupGuid);
        }

        /// <summary>
        /// Helper function that adds a new GroupServiceCarSchedule to the database
        /// </summary>
        /// <param name="groupservicecarsschedule">GroupServiceCarSchedule object</param>
        public void AddGroupServiceCarSchedule(GroupServiceCarSchedule groupservicecarsschedule)
        {
            GroupServiceCarSchedules.InsertOnSubmit(groupservicecarsschedule);
        }

        #endregion

        #region Employee

        /// <summary>
        /// Helper method to return all employees
        /// </summary>
        /// <returns>List with Employee objects</returns>
        public List<Employee> GetEmployees()
        {
            return this.Employees.ToList();
        }

        /// <summary>
        /// Helper method to return list with all service operations
        /// </summary>
        /// <param name="ServiceOperator">True/False value indicating if employee is a service operator</param>
        /// <returns>List with Car objects</returns>
        public List<Employee> GetEmployees(bool ServiceOperator)
        {
            return this.Employees.Where(p => p.ServiceOperator == ServiceOperator).ToList();
        }

        /// <summary>
        /// Helper method to return Employee object
        /// </summary>
        /// <param name="EmployeeId">Id on employee</param>
        /// <returns>Employee Object</returns>
        public Employee GetEmployee(int EmployeeId)
        {
            return this.Employees.SingleOrDefault(p => p.EmployeeId == EmployeeId);
        }

        /// <summary>
        /// Helper function that adds a new Employee to the database
        /// </summary>
        /// <param name="employee">Employee object</param>
        public void AddEmployee(Employee employee)
        {
            Employees.InsertOnSubmit(employee);
        }

        #endregion

        // OUTDATED: 2012-10-28, not used anymore - taken over by E-conomic
        //#region Invoice

        ///// <summary>
        ///// Helper method to return Invoice object by InvoiceGuidId
        ///// </summary>
        ///// <param name="InvoiceGuid">Guid Id for invoices</param>
        ///// <returns>Invoice Object</returns>
        //public Invoice GetInvoice(Guid InvoiceGuid)
        //{
        //    return this.Invoices.SingleOrDefault(p => p.InvoiceGuid == InvoiceGuid);
        //}

        ///// <summary>
        ///// Helper method to return Invoice object by InvoiceGuidId
        ///// </summary>
        ///// <param name="InvoiceId">Id for invoices</param>
        ///// <returns>Invoice Object</returns>
        //public Invoice GetInvoice(int InvoiceId)
        //{
        //    return this.Invoices.SingleOrDefault(p => p.InvoiceId == InvoiceId);
        //}

        ///// <summary>
        ///// Helper method to return all invoices
        ///// </summary>
        ///// <returns>List with Invoice objects</returns>
        //public List<Invoice> GetInvoices()
        //{
        //    return this.Invoices.ToList();
        //}

        ///// <summary>
        ///// Helper method to return invoices with a specific status
        ///// </summary>
        ///// <param name="InvoiceStatusId">Status on invoices</param>
        ///// <returns>List with Invoice objects</returns>
        //public List<Invoice> GetInvoices(int InvoiceStatusId)
        //{
        //    return this.Invoices.Where(p => p.InvoiceStatusId == InvoiceStatusId).ToList();
        //}

        ///// <summary>
        ///// Helper method to return Invoice objects for a specific customer
        ///// </summary>
        ///// <param name="CustomerId">Id on Customer</param>
        ///// <returns>List with Invoice objects</returns>
        //public List<Invoice> GetCustomerInvoices(int CustomerId)
        //{
        //    return this.Invoices.Where(p => p.CustomerId == CustomerId).ToList();
        //}

        ///// <summary>
        ///// Helper function that adds a new Invoice to the database
        ///// </summary>
        ///// <param name="invoice">Invoice object</param>
        //public void AddInvoice(Invoice invoice)
        //{
        //    Invoices.InsertOnSubmit(invoice);
        //}

        //#endregion

        // OUTDATED: 2012-10-28, not used anymore - taken over by E-conomic
        //#region InvoiceDetail

        ///// <summary>
        ///// Helper method to return InvoiceDetail object
        ///// </summary>
        ///// <param name="InvoiceDetailId">Id on invoicedetail</param>
        ///// <returns>InvoiceDetail object</returns>
        //public InvoiceDetail GetInvoiceDetail(int InvoiceDetailId)
        //{
        //    return this.InvoiceDetails.SingleOrDefault(p => p.InvoiceDetailId == InvoiceDetailId);
        //}

        ///// <summary>
        ///// Helper method to return all invoicedetails for a specific invoice
        ///// </summary>
        ///// <param name="InvoiceId">Id on invoice</param>
        ///// <returns>List with InvoiceDetail objects</returns>
        //public List<InvoiceDetail> GetInvoiceDetails(int InvoiceId)
        //{
        //    return this.InvoiceDetails.Where(p => p.InvoiceId == InvoiceId).ToList();
        //}

        //public decimal SumInvoiceDetailsPrice(int InvoiceId)
        //{
        //    return this.InvoiceDetails.Where(p => p.InvoiceId == InvoiceId).Sum(p => p.Price);
        //}

        ///// <summary>
        ///// Helper method to delete invoicedetail from database
        ///// </summary>
        ///// <param name="invoicedetail">InvoiceDetail object</param>
        //public void RemoveInvoiceDetail(InvoiceDetail invoicedetail)
        //{
        //    InvoiceDetails.DeleteOnSubmit(invoicedetail);
        //}

        ///// <summary>
        ///// Helper function that adds a new InvoiceDetail to the database
        ///// </summary>
        ///// <param name="invoicedetail">InvoiceDetail object</param>
        //public void AddInvoiceDetail(InvoiceDetail invoicedetail)
        //{
        //    InvoiceDetails.InsertOnSubmit(invoicedetail);
        //}

        //#endregion

        // OUTDATED: 2012-10-28, not used anymore - taken over by E-conomic
        //#region InvoiceStatus

        ///// <summary>
        ///// Helper method to return all invoice status types
        ///// </summary>
        ///// <returns>List with invoicestatus objects</returns>
        //public List<InvoiceStatus> GetInvoiceStatus()
        //{
        //    return this.InvoiceStatus.ToList();
        //}

        ///// <summary>
        ///// Helper method that returns invoicestatus description for a specific invoice status id
        ///// </summary>
        ///// <param name="InvoiceStatusId">Id for invoicestatus</param>
        ///// <returns>String with invoice status description</returns>
        //public string GetInvoiceStatusDescription(int InvoiceStatusId)
        //{
        //    return this.InvoiceStatus.SingleOrDefault(p => p.InvoiceStatusId == InvoiceStatusId).Description;
        //}

        //#endregion

        #region Task

        /// <summary>
        /// Helper method to return specific task based on id
        /// </summary>
        /// <param name="TaskId">Id on task</param>
        /// <returns>Task objects</returns>
        public Task GetTask(int TaskId)
        {
            return this.Tasks.SingleOrDefault(p => p.TaskId == TaskId);
        }

        /// <summary>
        /// Helper method to return list of tasks for a specific customer
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <param name="IncludeChildCustomers">True/false value to indicate if child customers should also be shown</param>
        /// <returns>List with TaskDependencyView objects</returns>
        public List<TaskDependencyView> GetTasksByCustomer(int CustomerId, bool IncludeChildCustomers)
        {
            if (IncludeChildCustomers)
            {
                return this.TaskDependencyViews.Where(p => p.CustomerId == CustomerId | p.ParentCustomerId == CustomerId).ToList();
            }
            else
            {
                return this.TaskDependencyViews.Where(p => p.CustomerId == CustomerId).ToList();
            }
        }

        /// <summary>
        /// Helper method to return all Tasks - extended view
        /// </summary>
        /// <returns>List with TaskServiceDrivePlanningView objects</returns>
        public List<TaskServiceDrivePlanningView> GetTasksExtendedView()
        {
            return this.TaskServiceDrivePlanningViews.ToList();
        }

        /// <summary>
        /// Helper method to return all Tasks with a specific tasktype, container and tasktypestatus
        /// </summary>
        /// <param name="TaskType">Array list with tasktypes</param>
        /// <param name="Container">Array list with Container Id's</param>
        /// <param name="TaskTypeStatus">Array list with TaskTypeStatus</param>
        /// <returns>List with Task objects</returns>
        public List<TaskServiceDrivePlanningView> GetTasksByTaskTypeContainerIdAndTaskTypeStatus(int[] TaskType, int[] Container, int[] TaskTypeStatus)
        {
            return this.TaskServiceDrivePlanningViews.Where(c => TaskType.Contains(c.TaskTypeId)).Where(d => TaskTypeStatus.Contains(d.TaskTypeStatusId)).Where(e => Container.Contains(e.ContainerId ?? 0)).ToList();
        }

        /// <summary>
        /// Helper method to return all Tasks for a specific Car
        /// </summary>
        /// <param name="CarId">Id on Container</param>
        /// <returns>List with Task objects</returns>
        //public List<Task> GetTasksByCar(int CarId)
        public List<TaskDependencyView> GetTasksByCar(int CarId)
        {
            //return this.Tasks.Where(p => p.CarId == CarId).ToList();
            return this.TaskDependencyViews.Where(p => p.CarId == CarId).ToList();
        }

        /// <summary>
        /// Helper method to return Tasks with a specific tasktypestatus for a specific Car
        /// </summary>
        /// <param name="CarId">Id on Car</param>
        /// <param name="TaskTypeStatus">Array list with TaskTypeStatus</param>
        /// <returns>List with Task objects</returns>
        public List<Task> GetTasksByCarAndTaskTypeStatus(int CarId, int[] TaskTypeStatus)
        {
            return this.Tasks.Where(p => p.CarId == CarId).Where(d => TaskTypeStatus.Contains(d.TaskTypeStatusId)).ToList();
        }

        /// <summary>
        /// Helper method to return Tasks with a specific tasktypestatus for a specific Car
        /// </summary>
        /// <param name="CarId">Id on Car</param>
        /// <param name="TireSetId">Id on TireSet</param>
        /// <param name="TaskTypeStatus">Array list with TaskTypeStatus</param>
        /// <returns>List with Task objects</returns>
        public List<Task> GetTasksByCarTireSetAndTaskTypeStatus(int CarId, int TireSetId, int[] TaskTypeStatus)
        {
            return this.Tasks.Where(p => p.CarId == CarId & p.TireSetId == TireSetId).Where(d => TaskTypeStatus.Contains(d.TaskTypeStatusId)).ToList();
        }

        /// <summary>
        /// Helper method to return Tasks with a specific tasktypestatus for a specific TireSet
        /// </summary>
        /// <param name="TireSetId">Id on TireSet</param>
        /// <param name="TaskTypeStatus">Array list with TaskTypeStatus</param>
        /// <returns>List with Task objects</returns>
        public List<Task> GetTasksByTireSetAndTaskTypeStatus(int TireSetId, int[] TaskTypeStatus)
        {
            return this.Tasks.Where(p => p.TireSetId == TireSetId).Where(d => TaskTypeStatus.Contains(d.TaskTypeStatusId)).ToList();
        }

        /// <summary>
        /// Function to return List with Tasks that has or has NOT dependencies to other tasks
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <param name="CarId">Id on car</param>
        /// <param name="TireSetId">Id on tireset</param>
        /// <param name="DependencyExists">True/False value</param>
        /// <returns>List with Task view Objects</returns>
        public List<TaskDependencyView> GetTasksWithDependency(int CustomerId, int CarId, int TireSetId, bool DependencyExists)
        {
            if (DependencyExists)
	        {
                return this.TaskDependencyViews.Where(p => p.CustomerId == CustomerId & p.CarId == CarId & p.TireSetId == TireSetId & p.DependentTaskId != null).ToList();
	        }

            return this.TaskDependencyViews.Where(p => p.CustomerId == CustomerId & p.CarId == CarId & p.TireSetId == TireSetId & p.DependentTaskId == null).ToList();
        }

        /// <summary>
        /// Function to return List with Tasks that has or has NOT ChildTask dependencies
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <param name="CarId">Id on car</param>
        /// <param name="TireSetId">Id on tireset</param>
        /// <param name="DependencyExists">True/False value</param>
        /// <returns>List with Task view Objects</returns>
        public List<TaskDependencyView> GetDependentChildTasks(int CustomerId, int CarId, int TireSetId, bool DependencyExists)
        {
            if (DependencyExists)
            {
                return this.TaskDependencyViews.Where(p => p.CustomerId == CustomerId & p.CarId == CarId & p.TireSetId == TireSetId & p.DependentChildTaskId != null).ToList();    
            }
            return this.TaskDependencyViews.Where(p => p.CustomerId == CustomerId & p.CarId == CarId & p.TireSetId == TireSetId & p.DependentChildTaskId == null).ToList();
        }

        // Udkommenteret da den er for specifik ifht. planlægningssiden
        //public List<TaskServiceDrivePlanningView> GetTaskServiceDrivePlannings(int TaskTypeId, int[] TaskTypeStatus)
        //{
        //    return this.TaskServiceDrivePlanningViews.Where(p => p.TaskTypeId == TaskTypeId).Where(d => TaskTypeStatus.Contains(d.TaskTypeStatusId)).ToList();
        //}

        /// <summary>
        /// Helper function that adds a new Task to the database
        /// </summary>
        /// <param name="task">Task object</param>
        public void AddTask(Task task)
        {
            Tasks.InsertOnSubmit(task);
        }

        #endregion

        #region TaskDetail

        ///// <summary>
        ///// Helper method to return all Tasks for a specific Car
        ///// </summary>
        ///// <param name="CarId">Id on Container</param>
        ///// <returns>List with Task objects</returns>
        //public List<Task> GetTasksByCar(int CarId)
        //{
        //    return this.Tasks.Where(p => p.CarId == CarId).ToList();
        //}

        /// <summary>
        /// Helper method to return specific taskdetail based on id
        /// </summary>
        /// <param name="TaskDetailId">Id on taskdetail</param>
        /// <returns>TaskDetail objects</returns>
        public TaskDetail GetTaskDetail(int TaskDetailId)
        {
            return this.TaskDetails.SingleOrDefault(p => p.TaskDetailId == TaskDetailId);
        }

        /// <summary>
        /// Helper method to return list of all taskdetails for a specific task
        /// </summary>
        /// <param name="TaskId">Id on task</param>
        /// <returns>List with TaskDetail Objects</returns>
        public List<TaskDetail> GetTaskDetails(int TaskId)
        {
            return this.TaskDetails.Where(p => p.TaskId == TaskId).ToList();
        }

        /// <summary>
        /// Helper method to return list of all taskdetails extended view for a specific task
        /// </summary>
        /// <param name="TaskId">Id on task</param>
        /// <returns>List with TaskDetailView Objects</returns>
        public List<TaskDetailView> GetTaskDetailsExtendedView(int TaskId)
        {
            return this.TaskDetailViews.Where(p => p.TaskId == TaskId).ToList();
        }

        /// <summary>
        /// Helper method to return count number for taskdetails for a specific task
        /// </summary>
        /// <param name="TaskId">Id on task</param>
        /// <returns>Count number for taskdetails</returns>
        public int CountTaskDetails(int TaskId)
        {
            return this.TaskDetails.Where(p => p.TaskId == TaskId).Count();
        }

        public int CountOpenTaskDetails(int TaskId) 
        {
            return this.TaskDetails.Where(p => p.TaskId == TaskId & ((p.Completed == true & p.Cancelled == false) | (p.Completed == false & p.Cancelled == true))).Count();
        }

        /// <summary>
        /// Helper method to return count number of taskdetails with completed and cancelled set either as True or False for a specific task
        /// </summary>
        /// <param name="TaskId">Id on task</param>
        /// <param name="Completed">True/false value</param>
        /// <param name="Cancelled">True/false value</param>
        /// <returns>Count number for taskdetails</returns>
        public int CountTaskDetailsCompletedOrCancelled(int TaskId, bool Completed, bool Cancelled)
        {
            return this.TaskDetails.Where(p => p.TaskId == TaskId & p.Completed == Completed & p.Cancelled == Cancelled).Count();
        }

        public int CountTireSetActiveTaskDetails(int TireSetId, int TaskDetailTypeId, int[] TaskTypeStatus) 
        {
            return this.TaskCombinedTaskDetailViews.Where(p => p.TireSetId == TireSetId & p.TaskDetailTypeId == TaskDetailTypeId & p.Completed == false & p.Cancelled == false).Where(d => TaskTypeStatus.Contains(d.TaskTypeStatusId)).Count();
        }

        //public int CountCarActiveTaskDetails(int CarId, int TaskDetailTypeId, int[] TaskTypeStatus)
        //{
        //    return this.TaskCombinedTaskDetailViews.Where(p => p.CarId == CarId & p.TaskDetailTypeId == TaskDetailTypeId & p.Completed == false & p.Cancelled == false).Where(d => TaskTypeStatus.Contains(d.TaskTypeStatusId)).Count();
        //}

        /// <summary>
        /// Helper function that adds a new TaskDetail to the database
        /// </summary>
        /// <param name="taskdetail">TaskDetail object</param>
        public void AddTaskDetail(TaskDetail taskdetail)
        {
            TaskDetails.InsertOnSubmit(taskdetail);
        }

        #endregion

        #region TaskLog

        /// <summary>
        /// Helper function that adds a new TaskLog to the database
        /// </summary>
        /// <param name="tasklog">TaskLog object</param>
        public void AddTaskLog(TaskLog tasklog)
        {
            TaskLogs.InsertOnSubmit(tasklog);
        }

        #endregion

        #region TaskType

        /// <summary>
        /// Helper method to return all TaskTypes
        /// </summary>
        /// <returns>List with TaskType objects</returns>
        public List<TaskType> GetTaskTypes()
        {
            return this.TaskTypes.ToList();
        }

        /// <summary>
        /// Helper method that returns TaskType description for a specific TaskType id
        /// </summary>
        /// <param name="TaskTypeId">Id for tasktype description</param>
        /// <returns>String with TaskType description</returns>
        public string GetTaskTypeDescription(int TaskTypeId)
        {
            return this.TaskTypes.SingleOrDefault(p => p.TaskTypeId == TaskTypeId).Description;
        }

        #endregion

        #region TaskTypeStatus

        /// <summary>
        /// Helper method to return all TaskTypeStatus
        /// </summary>
        /// <returns>List with TaskTypeStatus objects</returns>
        public List<TaskTypeStatus> GetTaskTypeStatus()
        {
            return this.TaskTypeStatus.ToList();
        }

        /// <summary>
        /// Helper method to return all TaskTypeStatus for a specific tasktype
        /// </summary>
        /// <param name="TaskTypeId">Id for tasktype to return tasktype status'</param>
        /// <returns>List with TaskTypeStatus objects</returns>
        public List<TaskTypeStatus> GetTaskTypeStatus(int TaskTypeId)
        {
            return this.TaskTypeStatus.Where(p => p.TaskTypeId == TaskTypeId).ToList();
        }

        /// <summary>
        /// Helper method that returns TaskType description for a specific TaskType id
        /// </summary>
        /// <param name="TaskTypeStatusId">Id for tasktypestatus description</param>
        /// <returns>String with TaskTypeStatus description</returns>
        public string GetTaskTypeStatusDescription(int TaskTypeStatusId)
        {
            return this.TaskTypeStatus.SingleOrDefault(p => p.TaskTypeStatusId == TaskTypeStatusId).Description;
        }

        #endregion

        #region TaskDetailType

        /// <summary>
        /// Helper method to return all TaskDetailTypes
        /// </summary>
        /// <returns>List with TaskDetailType objects</returns>
        public List<TaskDetailType> GetTaskDetailTypes()
        {
            return this.TaskDetailTypes.ToList();
        }

        /// <summary>
        /// Helper method to return all TaskDetailTypes within a specific TaskType
        /// </summary>
        /// <param name="TaskTypeId">Id for tasktype</param>
        /// <returns>List with TaskDetailType objects</returns>
        public List<TaskDetailType> GetTaskDetailTypes(int TaskTypeId)
        {
            return this.TaskDetailTypes.Where(p => p.TaskTypeId == TaskTypeId).ToList();
        }

        /// <summary>
        /// Helper method that returns TaskDetailType description for a specific TaskDetailType id
        /// </summary>
        /// <param name="TaskDetailTypeId">Id for taskdetailtype description</param>
        /// <returns>String with TaskDetailType description</returns>
        public string GetTaskDetailTypeDescription(int TaskDetailTypeId)
        {
            return this.TaskDetailTypes.SingleOrDefault(p => p.TaskDetailTypeId == TaskDetailTypeId).Description;
        }

        #endregion

        #region ServiceGeographicalArea

        /// <summary>
        /// Helper method to return all service geographical area
        /// </summary>
        /// <returns>List with ServiceGeographicalArea objects</returns>
        public List<ServiceGeographicalArea> GetServiceGeographicalAreas()
        {
            return this.ServiceGeographicalAreas.ToList();
        }

        /// <summary>
        /// Helper method to return ServiceGeographicalArea object
        /// </summary>
        /// <param name="ServiceGeoAreaId">Id on ServiceGeographicalArea</param>
        /// <returns>ServiceGeographicalArea Object</returns>
        public ServiceGeographicalArea GetServiceGeographicalArea(int ServiceGeoAreaId)
        {
            return this.ServiceGeographicalAreas.SingleOrDefault(p => p.ServiceGeoAreaId == ServiceGeoAreaId);
        }

        /// <summary>
        /// Helper method to return ServiceGeographicalArea object
        /// </summary>
        /// <param name="ServiceGeoAreaId">Id on ServiceGeographicalArea</param>
        /// <returns>String with Description</returns>
        public string GetServiceGeographicalAreaDescription(int ServiceGeoAreaId)
        {
            return this.ServiceGeographicalAreas.SingleOrDefault(p => p.ServiceGeoAreaId == ServiceGeoAreaId).Description;
        }

        /// <summary>
        /// Helper method to return ServiceGeographicalArea object
        /// </summary>
        /// <param name="ServiceGeoAreaId">Id on ServiceGeographicalArea</param>
        /// <returns>Integer with CountryId</returns>
        public int GetServiceGeographicalAreaCountryId(int ServiceGeoAreaId)
        {
            return this.ServiceGeographicalAreas.SingleOrDefault(p => p.ServiceGeoAreaId == ServiceGeoAreaId).CountryId;
        }

        /// <summary>
        /// Helper function that adds a new ServiceGeographicalArea to the database
        /// </summary>
        /// <param name="area">ServiceGeographicalArea object</param>
        public void AddServiceGeographicalArea(ServiceGeographicalArea area)
        {
            ServiceGeographicalAreas.InsertOnSubmit(area);
        }

        #endregion

        #region ServiceGeographicalAreaZipCodeList

        /// <summary>
        /// Helper method to return all zipcode for a specific service geographical area
        /// </summary>
        /// <param name="ServiceGeoAreaId">Id on ServiceGeographicalArea</param>
        /// <returns>List with ServiceGeographicalArea objects</returns>
        public List<ServiceGeographicalAreaZipCodeListView> GetServiceGeographicalAreaZipCodeLists(int ServiceGeoAreaId)
        {
            return this.ServiceGeographicalAreaZipCodeListViews.Where(p => p.ServiceGeoAreaId == ServiceGeoAreaId).ToList();
        }

        /// <summary>
        /// Helper method to return ServiceGeographicalAreaZipCodeList object
        /// </summary>
        /// <param name="AreaZipCodeId">Id on ServiceGeographicalAreaZipCodeList</param>
        /// <returns>ServiceGeographicalArea Object</returns>
        public ServiceGeographicalAreaZipCodeList GetServiceGeographicalAreaZipCodeList(int AreaZipCodeId)
        {
            return this.ServiceGeographicalAreaZipCodeLists.SingleOrDefault(p => p.AreaZipCodeId == AreaZipCodeId);
        }

        /// <summary>
        /// Helper method to delete ServiceGeographicalAreaZipCodeList from database
        /// </summary>
        /// <param name="areazipcode">ServiceGeographicalAreaZipCodeList object</param>
        public void RemoveServiceGeographicalAreaZipCodeList(ServiceGeographicalAreaZipCodeList areazipcode)
        {
            ServiceGeographicalAreaZipCodeLists.DeleteOnSubmit(areazipcode);
        }

        public int CountZipCodeInServiceGeographicalArea(int ServiceGeoAreaId, string ZipCode) 
        {
            return this.ServiceGeographicalAreaZipCodeLists.Where(p => p.ServiceGeoAreaId == ServiceGeoAreaId & p.ZipCode == ZipCode).Count();
        }

        /// <summary>
        /// Helper function that adds a new ServiceGeographicalAreaZipCodeList to the database
        /// </summary>
        /// <param name="areazipcode">ServiceGeographicalAreaZipCodeList object</param>
        public void AddServiceGeographicalAreaZipCodeList(ServiceGeographicalAreaZipCodeList areazipcode)
        {
            ServiceGeographicalAreaZipCodeLists.InsertOnSubmit(areazipcode);
        }

        #endregion

        #region ServiceCarGeographicalAreaAllocation

        /// <summary>
        /// Helper method to return geographical area allocations to a specific servicecar
        /// </summary>
        /// <param name="ServiceCarId">Id on servicecar</param>
        /// <returns>List with ServiceCarGeographicalAreaAllocation objects</returns>
        public List<ServiceCarGeographicalAreaAllocation> GetServiceCarGeographicalAreaAllocationLists(int ServiceCarId)
        {
            return this.ServiceCarGeographicalAreaAllocations.Where(p => p.ServiceCarId == ServiceCarId).ToList();
        }

        /// <summary>
        /// Helper method to return ServiceCarGeographicalAreaAllocation object
        /// </summary>
        /// <param name="ServiceCarAreaAllocationId">Id on ServiceCarAreaAllocation</param>
        /// <returns>ServiceCarGeographicalAreaAllocation Object</returns>
        public ServiceCarGeographicalAreaAllocation GetServiceCarGeographicalAreaAllocation(int ServiceCarAreaAllocationId)
        {
            return this.ServiceCarGeographicalAreaAllocations.SingleOrDefault(p => p.ServiceCarAreaAllocationId == ServiceCarAreaAllocationId);
        }

        /// <summary>
        /// Helper method to return ServiceCarGeographicalAreaAllocation object
        /// </summary>
        /// <param name="ServiceCarId">Id on ServiceCar</param>
        /// <param name="ScheduleDate">Service scheduled date</param>
        /// <returns>ServiceCarGeographicalAreaAllocation Object</returns>
        public ServiceCarGeographicalAreaAllocation GetServiceCarGeographicalAreaAllocation(int ServiceCarId, DateTime ScheduleDate)
        {
            return this.ServiceCarGeographicalAreaAllocations.SingleOrDefault(p => p.ServiceCarId == ServiceCarId & p.ScheduleDate == ScheduleDate);
        }

        /// <summary>
        /// Helper method to delete ServiceCarGeographicalAreaAllocation from database
        /// </summary>
        /// <param name="servicecararea">ServiceCarGeographicalAreaAllocation object</param>
        public void RemoveServiceCarGeographicalAreaAllocation(ServiceCarGeographicalAreaAllocation servicecararea)
        {
            ServiceCarGeographicalAreaAllocations.DeleteOnSubmit(servicecararea);
        }

        /// <summary>
        /// Helper function that adds a new ServiceCarGeographicalAreaAllocation to the database
        /// </summary>
        /// <param name="servicecararea">ServiceCarGeographicalAreaAllocation object</param>
        public void AddServiceCarGeographicalAreaAllocation(ServiceCarGeographicalAreaAllocation servicecararea)
        {
            ServiceCarGeographicalAreaAllocations.InsertOnSubmit(servicecararea);
        }

        #endregion

        #region CountryZipCodeList

        /// <summary>
        /// Helper method to return all zipcodes and cities for a specific country
        /// </summary>
        /// <param name="CountryId">Id on Country</param>
        /// <returns>List with CountryZipCodeList objects</returns>
        public List<CountryZipCodeList> GetCountryZipCodeLists(int CountryId)
        {
            return this.CountryZipCodeLists.Where(p => p.CountryId == CountryId).ToList();
        }

        #endregion

        #region TirePriceListUpdate

        /// <summary>
        /// Helper method to return all tireprice updates
        /// </summary>
        /// <returns>List of TirePriceListUpdate Objects</returns>
        public List<TirePriceListUpdate> GetTirePriceListUpdates()
        {
            return this.TirePriceListUpdates.ToList();
        }

        public int GetLatestTirePriceList()
        {
            return this.TirePriceListUpdates.Max(p => p.TirePriceListUpdateId);
        }

        /// <summary>
        /// Helper function that adds a new TirePriceListUpdate to the database
        /// </summary>
        /// <param name="pricelistupdate">TirePriceListUpdate object</param>
        public void AddTirePriceListUpdate(TirePriceListUpdate pricelistupdate)
        {
            TirePriceListUpdates.InsertOnSubmit(pricelistupdate);
        }

        #endregion

        #region TirePriceList

        /// <summary>
        /// Helper method to return list of tireprices
        /// </summary>
        /// <param name="TirePriceListUpdateId">Id on tireprice update</param>
        /// <returns>List of TirePriceList Objects</returns>
        public List<TirePriceList> GetTirePriceLists(int TirePriceListUpdateId)
        {
            return this.TirePriceLists.Where(p => p.TirePriceListUpdateId == TirePriceListUpdateId).ToList();
        }

        /// <summary>
        /// Helper method to return TirePriceList object
        /// </summary>
        /// <param name="TirePriceListId">Id on tireprice object</param>
        /// <returns>TirePriceList Object</returns>
        public TirePriceList GetTirePriceList(int TirePriceListId)
        {
            return this.TirePriceLists.SingleOrDefault(p => p.TirePriceListId == TirePriceListId);
        }

        /// <summary>
        /// Helper method to return list of tireprices for a specific tire dimension, load and speed
        /// </summary>
        /// <param name="TirePriceListUpdateId">Id on tireprice update</param>
        /// <param name="Width">Width of tire</param>
        /// <param name="Height">Height of tire</param>
        /// <param name="Size">Size of tire</param>
        /// <param name="LoadIndex">LoadIndex of tire</param>
        /// <param name="SpeedIndex">SpeedIndex of tire</param>
        /// <returns>List of TirePriceList Objects</returns>
        public List<TirePriceList> GetTirePriceListSpecificTires(int TirePriceListUpdateId, decimal Width, decimal Height, decimal Size, int LoadIndex, int SpeedIndex)
        {
            return this.TirePriceLists.Where(p => p.TirePriceListUpdateId == TirePriceListUpdateId & p.Width == Width & p.Height == Height & p.Size == Size & p.TireLoadIndexId == LoadIndex & p.TireSpeedIndexId == SpeedIndex).ToList();
        }

        /// <summary>
        /// Helper method to return tire cost price
        /// </summary>
        /// <param name="TirePriceListId">Id on TirePriceList record</param>
        /// <returns>Decimal with tire cost price from vendor</returns>
        public decimal GetTirePriceCostPriceValue(int TirePriceListId)
        {
            return this.TirePriceLists.SingleOrDefault(p => p.TirePriceListId == TirePriceListId).CostPrice ?? 0;
        }

        /// <summary>
        /// Helper function that adds a new TirePriceList to the database
        /// </summary>
        /// <param name="pricelist">TirePriceList object</param>
        public void AddTirePriceList(TirePriceList pricelist)
        {
            TirePriceLists.InsertOnSubmit(pricelist);
        }

        #endregion

        #region TireQuotation

        /// <summary>
        /// Helper method to return all active tire quotations where no respons has been given from the customer
        /// </summary>
        /// <returns>List with TireQuotation objects</returns>
        public List<TireQuotation> GetTireQuotationActive()
        {
            return this.TireQuotations.Where(p => p.Approved != null & p.Rejected == null & p.Expiredate > DateTime.Now).ToList();
        }

        /// <summary>
        /// Helper function that adds a new TireQuotation to the database
        /// </summary>
        /// <param name="tirequotation">TireQuotation object</param>
        public void AddTireQuotation(TireQuotation tirequotation)
        {
            TireQuotations.InsertOnSubmit(tirequotation);
        }

        #endregion

        #region TireQuotationCalculation

        /// <summary>
        /// Helper function that adds a new TireQuotationCalculation to the database
        /// </summary>
        /// <param name="quotationcalculation">TireQuotationCalculation object</param>
        public void AddTireQuotationCalculation(TireQuotationCalculation quotationcalculation)
        {
            TireQuotationCalculations.InsertOnSubmit(quotationcalculation);
        }

        #endregion

        #region PartnerListEDI

        /// <summary>
        /// Helper method to return list all EDI partners
        /// </summary>
        /// <returns>List of PartnerListEDI Objects</returns>
        public List<PartnerListEDI> GetPartnerListEDI()
        {
            return this.PartnerListEDIs.ToList();
        }

        /// <summary>
        /// Helper method to return partner object
        /// </summary>
        /// <param name="PartnerId">Id on partner</param>
        /// <returns>PartnerListEDI object</returns>
        public PartnerListEDI GetPartnerEDI(int PartnerId)
        {
            return this.PartnerListEDIs.SingleOrDefault(p => p.PartnerId == PartnerId);
        }

        /// <summary>
        /// Helper function that adds a new PartnerListEDI to the database
        /// </summary>
        /// <param name="partnerlistedi">PartnerListEDI object</param>
        public void AddPartnerListEDI(PartnerListEDI partnerlistedi)
        {
            PartnerListEDIs.InsertOnSubmit(partnerlistedi);
        }

        #endregion

        #region PartnerSubscriptionPrice

        /// <summary>
        /// Helper method to return subscription prices for a specific EDI partner
        /// </summary>
        /// <param name="PartnerId">Id on partner</param>
        /// <returns>List of PartnerSubscriptionPrice Objects</returns>
        public List<PartnerSubscriptionPrice> GetPartnerSubscriptionPrices(int PartnerId)
        {
            return this.PartnerSubscriptionPrices.Where(p => p.PartnerId == PartnerId).ToList();
        }

        /// <summary>
        /// Helper method to return PartnerSubscriptionPrice object for a specific partner and subscription
        /// </summary>
        /// <param name="PartnerId">Id on partner</param>
        /// <param name="SubscriptionId">Id on subscription</param>
        /// <returns>PartnerSubscriptionPrice object</returns>
        public PartnerSubscriptionPrice GetPartnerSubscriptionPrice(int PartnerId, int SubscriptionId)
        {
            return this.PartnerSubscriptionPrices.SingleOrDefault(p => p.PartnerId == PartnerId & p.SubscriptionId == SubscriptionId);
        }

        /// <summary>
        /// Helper method to return subscription price for a specific partner
        /// </summary>
        /// <param name="PartnerId">Id on partner</param>
        /// <param name="SubscriptionId">Id on subscription</param>
        /// <returns>Decimal with subscription price</returns>
        public decimal GetPartnerSubscriptionPriceValue(int PartnerId, int SubscriptionId)
        {
            return this.PartnerSubscriptionPrices.SingleOrDefault(p => p.PartnerId == PartnerId & p.SubscriptionId == SubscriptionId).Price;
        }

        /// <summary>
        /// Helper function that adds a new PartnerSubscriptionPrice to the database
        /// </summary>
        /// <param name="partnersubscriptionprice">PartnerSubscriptionPrice object</param>
        public void AddPartnerSubscriptionPrice(PartnerSubscriptionPrice partnersubscriptionprice)
        {
            PartnerSubscriptionPrices.InsertOnSubmit(partnersubscriptionprice);
        }

        #endregion

        #region ScheduledJobQueue

        /// <summary>
        /// Helper method to return scheduled job
        /// </summary>
        /// <param name="ScheduledJobQueueId">Id on scheduledjob</param>
        /// <returns>ScheduledJobQueue object</returns>
        public ScheduledJobQueue GetScheduledJob(int ScheduledJobQueueId)
        {
            return this.ScheduledJobQueues.SingleOrDefault(p => p.ScheduledJobQueueId == ScheduledJobQueueId);
        }

        /// <summary>
        /// Helper method to return all pending scheduled jobs that are overdue for a specific executiontime
        /// </summary>
        /// <param name="ExecutionTime">Time job must be before if they should be selected</param>
        /// <returns>List with ScheduledJobQueue objects</returns>
        public List<ScheduledJobQueue> GetScheduledJobsOverduePending(DateTime ExecutionTime)
        {
            return this.ScheduledJobQueues.Where(p => p.RealisedExecutionTime == null & p.ScheduledExecutionTime <= ExecutionTime).ToList();
        }

        /// <summary>
        /// Helper method to return all pending scheduled jobs that are not realised yet
        /// </summary>
        /// <returns>List with ScheduledJobQueue objects</returns>
        public List<ScheduledJobQueue> GetScheduledJobsPending()
        {
            return this.ScheduledJobQueues.Where(p => p.RealisedExecutionTime == null).ToList();
        }

        /// <summary>
        /// Helper method to return all scheduled jobs
        /// </summary>
        /// <returns>List with ScheduledJobQueue objects</returns>
        public List<ScheduledJobQueue> GetScheduledJobs()
        {
            return this.ScheduledJobQueues.ToList();
        }

        /// <summary>
        /// Helper function that removes a ScheduledJobQueue from the database
        /// </summary>
        /// <param name="scheduledjobqueue">ScheduledJobQueue object</param>
        public void RemoveScheduledJobQueue(ScheduledJobQueue scheduledjobqueue) 
        {
            ScheduledJobQueues.DeleteOnSubmit(scheduledjobqueue);
        }

        /// <summary>
        /// Helper function that adds a new ScheduledJobQueue to the database
        /// </summary>
        /// <param name="scheduledjobqueue">ScheduledJobQueue object</param>
        public void AddScheduledJobQueue(ScheduledJobQueue scheduledjobqueue)
        {
            ScheduledJobQueues.InsertOnSubmit(scheduledjobqueue);
        }

        #endregion

        #region ExternalOrderInterface

        /// <summary>
        /// Helper method to return all open orders from interface for a specific customer
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <returns>List with ExternalOrderInterface objects</returns>
        public List<ExternalOrderInterface> GetExternalOpenOrdersByCustomer(int CustomerId)
        {
            return this.ExternalOrderInterfaces.Where(p => p.CustomerId == CustomerId & p.Finalisedate == null).ToList();
        }

        /// <summary>
        /// Helper method to return all open orders from interface for a specific customer and subscription
        /// </summary>
        /// <param name="CustomerId">Id on customer</param>
        /// <param name="SubscriptionId">Id on subscription</param>
        /// <returns>List with ExternalOrderInterface objects</returns>
        public List<ExternalOrderInterface> GetExternalOpenOrdersByCustomerAndSubscription(int CustomerId, int SubscriptionId)
        {
            return this.ExternalOrderInterfaces.Where(p => p.CustomerId == CustomerId & p.SubscriptionId == SubscriptionId & p.Finalisedate == null).ToList();
        }

        #endregion

    }
}
